@@ -98,26 +98,29 @@ format_chr_deparse <- function(x) {
98
98
paste(collapse = " " , deparse(x ))
99
99
}
100
100
101
- # ' Format a character vector as a string via deparsing/quoting each
101
+ # ' Format each entry in a character vector via quoting; special replacement for length 0
102
+ # '
103
+ # ' Performs no escaping within the strings; if you want something that reader
104
+ # ' could copy-paste to debug, look into `format_deparse` (note that this
105
+ # ' collapses into a single string).
106
+ # '
107
+ # ' @param x chr; e.g., `colnames` of some data frame
108
+ # ' @param empty chr, likely string; what should be output if `x` is of length 0?
109
+ # ' @return chr; same `length` as `x` if `x` had nonzero length; value of `empty` otherwise
110
+ # '
111
+ # ' @examples
112
+ # ' cli::cli_inform('{format_chr_with_quotes("x")}')
113
+ # ' cli::cli_inform('{format_chr_with_quotes(c("x","y"))}')
114
+ # ' nms <- c("x","\"Total Cases\"")
115
+ # ' cli::cli_inform('{format_chr_with_quotes(nms)}')
116
+ # ' cli::cli_inform('{format_chr_with_quotes(character())}')
102
117
# '
103
- # ' @param x `chr`; e.g., `colnames` of some data frame
104
- # ' @param empty string; what should be output if `x` is of length 0?
105
- # ' @return string
106
118
# ' @keywords internal
107
119
format_chr_with_quotes <- function (x , empty = " *none*" ) {
108
120
if (length(x ) == 0L ) {
109
121
empty
110
122
} else {
111
- # Deparse to get quoted + escape-sequenced versions of varnames; collapse to
112
- # single line (assuming no newlines in `x`). Though if we hand this to cli
113
- # it may insert them (even in middle of quotes) while wrapping lines.
114
- deparsed_collapsed <- paste(collapse = " " , deparse(x ))
115
- if (length(x ) == 1L ) {
116
- deparsed_collapsed
117
- } else {
118
- # remove surrounding `c()`:
119
- substr(deparsed_collapsed , 3L , nchar(deparsed_collapsed ) - 1L )
120
- }
123
+ paste0(' "' , x , ' "' )
121
124
}
122
125
}
123
126
0 commit comments