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"