Quote elements of a vector and concatenate the result to a single character string.
Arguments
- x
Dimensionless atomic object to be converted to a single character string.
- collapse
Non-empty character string used to separate the quoted elements of
x.
Value
A character string consisting of the elements of x surrounded by single
quotes, separated by the string given in collapse. See Details on the
handling of some special values in x.
Details
Some values are handled specially to print clearer:
NULLis returned as"'NULL'"non-
NULLzero-length objects are returned as"'<class>(0)'", e.g.,"'logical(0)'"""is returned as'""'logical
NAis returned as"'NA'"non-logical
NAis returned as"'NA_<class>_'"(e.g.,"'NA_real_'"; for factors this is"'NA_character_'").
Notes
An error is thrown if multiple arguments are provided because then x
probably was accidentally not combined. For example, the call
paste_quoted("a", "b") will return the error unused argument ("b"). The
probably intended call is paste_quoted(c("a", "b")), returning "'a', 'b'".
names of x are dropped by paste_quoted(), with a
warning. Use unname() on named x to prevent such warnings
and use progutils::vect_to_char(x) for numeric x to preserve names.
See also
toString() which is shorthand for paste(x, collapse = ", ");
Quotes and sQuote() for documentation on quotes;
paste0();
progutils::unpaste_unquote() for the approximate opposite of paste_quoted();
progutils::vect_to_char() to preserve names of numeric x
Examples
paste_quoted(c(3, 4)) # "'3', '4'"
#> [1] "'3', '4'"
paste_quoted(NULL) # "'NULL'"
#> [1] "'NULL'"
paste_quoted(c(a = 3, b = 4)) # "'3', '4'" # Warns about dropping names.
#> Warning: 'x' has names, these will be discarded.
#> Use progutils::vect_to_char() instead of paste_quoted() to preserve names of numeric 'x'.
#> [1] "'3', '4'"
paste_quoted(c(3, 4), collapse = "\n") # separate '3' and '4' by a newline
#> [1] "'3'\n'4'"