简体   繁体   中英

R retain strings when converting vectors to character vectors to submit to API

Ok, I suspect the answer to this question will uncover my ignorance but here it goes:

I have this input_vector of strings

input_vector <-c("string1","string2")

And I need to pass these items to a very complex API query which requires the " to be included. If I do this stringi::stri_paste(input_vector, collapse = ',') then the output is not usable

"string1,string2"

What I really need is

"string1","string2"

How do I do this?

paste(shQuote(input_vector), collapse = ",")

You can use:

paste0('"', input_vector, '"', collapse = ',')
#[1] "\"string1\",\"string2\""

To view the actual string use cat :

cat(paste0('"', input_vector, '"', collapse = ','))
#"string1","string2"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM