1

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"

am3010
  • 57
  • 6
  • 2
    The question needs to explain what the expected output is but maybe you want `cat(paste0("url", toString(sprintf("'%s'", vector)), "url"), "\n")` or maybe `paste0("url", toString(sprintf("'%s'", vector)), "url")` depending on what you want. Do not put `cat` in the middle of an expression. – G. Grothendieck Jul 08 '22 at 14:21

4 Answers4

3

What about:

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

Output:

'apple', 'banana', 'cherry'
harre
  • 7,081
  • 2
  • 16
  • 28
  • how could I modify this to use double quotes instead of single quotes in my output? I'm having trouble with the syntax because I can't wrap " using " " – Brenda Thompson Jul 26 '23 at 20:35
2

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)

Quinten
  • 35,235
  • 5
  • 20
  • 53
2

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'
Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
0

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")
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341