Pass vector as a chain of strings R - 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'"

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"

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"

Extract a pattern before // and after || symbol

I am not very familiar with regex in R.
in a column I am trying to extract words before // and after || symbol. I.e. this is what I have in my column:
qtaro_269//qtaro_269||qtaro_353//qtaro_353||qtaro_375//qtaro_375||qtaro_11//qtaro_11
This is what I want:
qtaro_269; qtaro_353; qtaro_375; qtaro_11
I found this: Extract character before and after "/" and this: Extract string before "|". However I don't know how to adjust it to my input. Any hint is much appreciated.
EDIT:
a qtaro_269//qtaro_269||qtaro_353//qtaro_353||qtaro_375//qtaro_375||qtaro_11//qtaro_11
b
c qtaro_269//qtaro_269||qtaro_353//qtaro_353||qtaro_375//qtaro_375||qtaro_11//qtaro_11
What about the following?
# Split by "||"
x2 <- unlist(strsplit(x, "\\|\\|"))
[1] "qtaro_269//qtaro_269" "qtaro_353//qtaro_353" "qtaro_375//qtaro_375" "qtaro_11//qtaro_11"
# Remove everything before and including "//"
gsub(".+//", "", x2)
[1] "qtaro_269" "qtaro_353" "qtaro_375" "qtaro_11"
And if you want it as one string with ; for separation:
paste(gsub(".+//", "", x2), collapse = "; ")
[1] "qtaro_269; qtaro_353; qtaro_375; qtaro_11"
This is how I solved it. For sure not the most intelligent and elegant way, so suggestions to improve it are welcome.
df <-unlist(lapply(strsplit(df[[2]],split="\\|\\|"), FUN = paste, collapse = "; "))
df <-unlist(lapply(strsplit(df[[2]],split="\\/\\/"), FUN = paste, collapse = "; "))
df <- sapply(strsplit(df$V2, "; ", fixed = TRUE), function(x) paste(unique(x), collapse = "; "))

pasting a mixed list and keep quotes on strings

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.

Resources