Home > Software design >  In R, print vector in single quotes and comma separated
In R, print vector in single quotes and comma separated

Time:07-09

Problem Statement: I'm creating a dynamic application in which user select inputs and they are passed into URL to filter data. User can select single or multiple values. I'm using knitr::combine_words(Selected_Input, before = ",", and = "", sep = ",") to get them in single quotes and comma separated. But facing issue when user selects single value (as described below):

#User selecting multiple values
Selected_Input <- c("Apple","Banana","Cherry")
knitr::combine_words(Selected_Input, before = ",", and = "", sep = ",")

Result: 'Apple','Banana','Cherry' which works for my code.

But when user selects single value

#User selecting single value
Selected_Input <- c("Apple")
knitr::combine_words(Selected_Input, before = ",", and = "", sep = ",")

Result: ,Apple, which doesn't work. As it should be single quoted.

I'm using this knitr::combine_words inside paste0 to create a dynamic URL. So I'm looking for a way which works inside paste0.

If I'm using cat() function inside paste0 then the output doesn't work in my code. The url doesn't fall in place.

vector <- c("apple", "banana", "cherry")
out <- paste(sQuote(vector, FALSE), collapse=", ")
cat(out, "\n")
#> 'apple', 'banana', 'cherry'

cat(toString(sQuote(vector, FALSE)))

paste0("url",cat(toString(sQuote(vector, FALSE))),"url")

Result: 'apple', 'banana', 'cherry'[1] "urlurl"

CodePudding user response:

Another option using sQuote:

Single or double quote text by combining with appropriate single or double left and right quotation marks.

vector <- c("apple", "banana", "cherry")
out <- paste(sQuote(vector, FALSE), collapse=", ")
cat(out, "\n")
#> 'apple', 'banana', 'cherry'

Created on 2022-07-08 by the reprex package (v2.0.1)

CodePudding user response:

What about:

fruits <- c("apple", "banana", "cherry")
all_fruit_in_one <- paste0(paste0("'", fruits, "'"), collapse = ", ")
cat(all_fruit_in_one)

Output:

'apple', 'banana', 'cherry'

CodePudding user response:

Use sprintf to insert the quotes and then use toString (assuming that comma with a space is acceptable as the separator). Optionally cat or print the result depending on exactly what you want; however, simply entering it into the console will print it.

toString(sprintf("'%s'", fruits))
## [1] "'apple', 'banana', 'cherry'"

toString(sprintf("'%s'", fruits[1]))
## [1] "'apple'"

This can also be expressed in terms of pipes:

fruits |> sprintf(fmt = "'%s'") |> toString()
## [1] "'apple', 'banana', 'cherry'"

Note

The input in reproducible form is assumed to be:

fruits <- c("apple", "banana", "cherry")

CodePudding user response:

I think it was just because of a typo in your code, i.e., it should be before = "'" instead of before = ",".

> Selected_Input <- c("Apple","Banana","Cherry")
> knitr::combine_words(Selected_Input, before = "'", and = "", sep = ",")
'Apple','Banana','Cherry'

> Selected_Input <- c("Apple")
> knitr::combine_words(Selected_Input, before = "'", and = "", sep = ",")
'Apple'
  • Related