pasting a mixed list and keep quotes on strings - r

This is basically the R equivalent of this question.
I have a list of mixed elements:
l = list(-1, "quicksort", NULL)
And I want to turn it into a string:
string = '-1, "quicksort", NULL'
But I can't figure out how to easily keep the quotes inside the string without putting ALL elements in quotes:
paste(l, collapse = ", ") # WRONG
# "-1, quicksort, NULL"
paste(shQuote(l), collapse = ", ") # WRONG
# '"-1", "quicksort", "NULL"'
I have a solution, but it seems clumsy:
paste(lapply(l, function(x) if(class(x) == "character") shQuote(x) else x),
collapse=", ")
# '-1, "quicksort", NULL'
Is there a simpler (i.e. no if statement) solution?

deparse() the list and then remove the unwanted characters.
gsub("list|[()]", "", deparse(l))
# [1] "-1, \"quicksort\", NULL"

My preferred solution ended up being
paste(lapply(l, deparse), collapse = ", ")
Which bypasses the need for gsub stuff and supports any type of list element. I think it's a bit more readable too.

Related

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"

Pass vector as a chain of strings R

I have the following vector:
vectr <- c("LIBDISP1","LIBDISP2","LIBDISP3")
and I want it as a chain of strings to use in a sql query.
"'LIBDISP1','LIBDISP2','LIBDISP3'"
I tried the following:
text <- paste(as.character(vectr), collapse = ", ")
But it returns:
"LIBDISP1, LIBDISP2, LIBDISP3"
Any help will be greatly appreciated.
We can use sQuote with paste
paste(sQuote(vectr, FALSE), collapse=', ')
#[1] "'LIBDISP1', 'LIBDISP2', 'LIBDISP3'"
or with toString
toString(sQuote(vectr, FALSE))
We can use paste0 like :
paste0("'", vectr, "'", collapse = ",")
#[1] "'LIBDISP1', 'LIBDISP2', 'LIBDISP3'"

paste0 is putting " in wrong place

I am creating columns of variables.
myVars=paste0("var",rep(1:5))
myVars
paste0(myVars,"=rnorm(5)")
output:
"var1=rnorm(5)" "var2=rnorm(5)" "var3=rnorm(5)" "var4=rnorm(5)"
"var5=rnorm(5)"
note the second quote should be after var1 as seen below.
I also want to paste in the comma seen in wanted output.
That should require something like paste0(A,B,C)
Want:
"var1"=rnorm(5), "var2"=rnorm(5), "var3"=rnorm(5), "var4"=rnorm(5),
"var5"=rnorm(5)
If we need to have double quotes around 'myVars', use dQuote with q = FALSE to avoid having the fancyquotes
out <- paste0(dQuote(myVars, q = FALSE), "=rnorm(5)")
cat(out, '\n')
#"var1"=rnorm(5) "var2"=rnorm(5) "var3"=rnorm(5) "var4"=rnorm(5) "var5"=rnorm(5)
if it should be a single string
out1 <- paste(dQuote(myVars, q = FALSE), "=rnorm(5)", sep="", collapse=", ")
cat(out1, '\n')
#"var1"=rnorm(5), "var2"=rnorm(5), "var3"=rnorm(5), "var4"=rnorm(5), "var5"=rnorm(5)

R - Construct a string with double quotations

I basically need the outcome (string) to have double quotations, thus need of escape character. Preferabily solving with R base, without extra R packages.
I have tried with squote, shQuote and noquote. They just manipulate the quotations, not the escape character.
My list:
power <- "test"
myList <- list (
"power" = power)
I subset the content using:
myList
myList$power
Expected outcome (a string with following content):
" \"power\": \"test\" "
Using package glue:
library(glue)
glue(' "{names(myList)}": "{myList}" ')
"power": "test"
Another option using shQuote
paste(shQuote(names(myList), type = "cmd"),
shQuote(unlist(myList), type = "cmd"),
sep = ": ")
# [1] "\"power\": \"test\""
Not sure to get your expectation. Is it what you want?
myList <- list (
"power" = "test"
)
stringr::str_remove_all(
as.character(jsonlite::toJSON(myList, auto_unbox = TRUE)),
"[\\{|\\}]")
# [1] "\"power\":\"test\""
If you want some spaces:
x <- stringr::str_remove_all(
as.character(jsonlite::toJSON(myList, auto_unbox = TRUE)),
"[\\{|\\}]")
paste0(" ", x, " ")

Replace multiple strings comprising of a different number of characters with one gsubfn()

Here Replace multiple strings in one gsub() or chartr() statement in R? it is explained to replace multiple strings of one character at in one statement with gsubfn(). E.g.:
x <- "doremi g-k"
gsubfn(".", list("-" = "_", " " = ""), x)
# "doremig_k"
I would however like to replace the string 'doremi' in the example with ''. This does not work:
x <- "doremi g-k"
gsubfn(".", list("-" = "_", "doremi" = ""), x)
# "doremi g_k"
I guess it is because of the fact that the string 'doremi' contains multiple characters and me using the metacharacter . in gsubfn. I have no idea what to replace it with - I must confess I find the use of metacharacters sometimes a bit difficult to udnerstand. Thus, is there a way for me to replace '-' and 'doremi' at once?
You might be able to just use base R sub here:
x <- "doremi g-k"
result <- sub("doremi\\s+([^-]+)-([^-]+)", "\\1_\\2", x)
result
[1] "g_k"
Does this work for you?
gsubfn::gsubfn(pattern = "doremi|-", list("-" = "_", "doremi" = ""), x)
[1] " g_k"
The key is this search: "doremi|-" which tells to search for either "doremi" or "-". Use "|" as the or operator.
Just a more generic solution to #RLave's solution -
toreplace <- list("-" = "_", "doremi" = "")
gsubfn(paste(names(toreplace),collapse="|"), toreplace, x)
[1] " g_k"

Resources