Reputation: 864
iris %>% select(where(is.numeric)) %>% colnames() %>% str_c(., collapse = '","')
Output: "Sepal.Length","Sepal.Width","Petal.Length","Petal.Width"
It gets me close to what I want (I don't really care about the space missing after the comma) but I can't remove the backslash afterwards with str_remove_all
because it doesn't recognize the escaped backslash
Ideal: "Sepal.Length","Sepal.Width","Petal.Length","Petal.Width"
iris %>% select(where(is.numeric)) %>% colnames() %>% str_c(., collapse = '",') str_remove_all(., "\\")
output: Error in stri_replace_all_regex(string, pattern, fix_replacement(replacement), : Unrecognized backslash escape sequence in pattern. (U_REGEX_BAD_ESCAPE_SEQUENCE)
Upvotes: 1
Views: 269
Reputation: 388817
You can use :
library(dplyr)
iris %>%
select(where(is.numeric)) %>%
colnames() %>%
sprintf('"%s"', .) %>%
toString()
#[1] "\"Sepal.Length\", \"Sepal.Width\", \"Petal.Length\", \"Petal.Width\""
The backslashes are not part of actual string, that is how R displays double quotes. To see the actual string you can use cat
:
iris %>%
select(where(is.numeric)) %>%
colnames() %>%
sprintf('"%s"', .) %>%
toString() %>%
cat
#"Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"
Upvotes: 1