Add single quote to start and end of the vector of a string which would be fed into sqldf
The sqldf package loads the gsubfn package providing fn
for this purpose. fn
supports backtick and dollar substitutions for code and single variables respectively. For example, try any of these. See ?fn
and the examples on the sqldf github page.
fn$sqldf("select * from df where b in ( `toString(shQuote(vec1, 'sh'))` ) ")
p <- fn$identity("select * from df where b in ( `toString(shQuote(vec1, 'sh'))` ) ")
sqldf(p)
s <- toString(shQuote(vec1, 'sh'))
p <- fn$identity("select * from df where b in ( $s ) ")
sqldf(p)
You can use sprintf
for this.
sprintf("'%s'", paste(vec1, collapse = "','"))
create a function will make it more readable...
addQuotes <- function(x) sprintf("'%s'", paste(x, collapse = "','"))
addQuotes(vec1)