Skip to contents

Converts an atomic vector into a string of comma separated elements.

Usage

csw(
  x,
  n = Inf,
  quote = TRUE,
  ellipsis = TRUE,
  and = FALSE,
  or = FALSE,
  period = FALSE,
  width = 70L
)

Arguments

x

(atomic vector)
Will be coerced to character, then converted into a string of comma separated elements. NA elements are coerced to the string "NA" with no special treatment.

n

(scalar integer: Inf; [1, Inf])
The number of elements in x to include before truncating. Set as Inf to never truncate.

quote

(scalar logical: TRUE)
Whether or not to wrap elements in quotes. Elements containing ' are wrapped in double quotes. Elements containing " are wrapped in single quotes. Elements containing both ' and " are wrapped in double quotes.

ellipsis

(scalar logical: TRUE)
Whether or not to append "..." when x is truncated by n.

and

(scalar logical: FALSE)
Whether or not to insert ", and" before the last element. Has no effect when x has fewer than two elements or when x is truncated to n elements. Cannot be TRUE when or = TRUE.

or

(scalar logical: FALSE)
Whether or not to insert ", or" before the last element. Has no effect when x has fewer than two elements or when x is truncated to n elements. Cannot be TRUE when and = TRUE.

period

(scalar logical: FALSE)
Whether or not to end the string with a period. Has no effect when x is truncated to n elements.

width

(scalar integer: 70L; [1, Inf])
The column number at which a new line should be inserted. Set as Inf to never insert a new line.

Value

A character vector of length 0 or 1.

Details

Elements are coerced to character with as.character(). Pre-format numeric values that require specific precision before passing to csw().

When and = TRUE or or = TRUE with three or more elements, a comma is included before the connector (Oxford-comma style), e.g. "a, b, and c". With exactly two elements, no comma is used, e.g. "a and b".

The period added by period = TRUE is appended after word wrapping, so the final line may exceed width by one character.

Examples

#----------------------------------------------------------------------------
# csw() example
#----------------------------------------------------------------------------
library(bkbase)
set.seed(1)

csw(letters)
#> [1] "'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',\n'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'"

c(
  "Lorem","ipsum","dolor","sit","amet","consectetur","adipiscing","elit",
  "sed","do","eiusmod","tempor","incididunt","ut","labore","et","dolore",
  "magna","aliqua","Ut","enim","ad","minim","veniam","quis","nostrud"
) |>
  csw() |>
  message()
#> 'Lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur', 'adipiscing',
#> 'elit', 'sed', 'do', 'eiusmod', 'tempor', 'incididunt', 'ut',
#> 'labore', 'et', 'dolore', 'magna', 'aliqua', 'Ut', 'enim', 'ad',
#> 'minim', 'veniam', 'quis', 'nostrud'

rnorm(30) |>
  round(digits = 1) |>
  csw(quote = FALSE, and = TRUE) |>
  message()
#> -0.6, 0.2, -0.8, 1.6, 0.3, -0.8, 0.5, 0.7, 0.6, -0.3, 1.5, 0.4, -0.6,
#> -2.2, 1.1, 0, 0, 0.9, 0.8, 0.6, 0.9, 0.8, 0.1, -2, 0.6, -0.1, -0.2,
#> -1.5, -0.5, and 0.4