I am trying to generate a bunch of SQL scripts using
> script<-"select * from ?x WHERE DATE(?y)> DATE_SUB(NOW(), INTERVAL 1 DAY)"
#<SQL> select * from 'state_transtions' WHERE DATE('transition_time')> DATE_SUB(NOW(), INTERVAL 1 DAY)
gsubfn in the gsubfn package can do substitutions. The regular expression here matches a question mark followed by word characters and then it uses the correspondences defined in the second argument to perform the substitutions on the part of the regular expression matched by the part within parentheses.
library(gsubfn) gsubfn("[?](\\w+)", list(x = x, y = y), script)
giving the following:
 "select * from state_transtions WHERE DATE(transition_time)> DATE_SUB(NOW(), INTERVAL 1 DAY)"
2) fn$ The gsubfn package also provides
fn$ which can prefix any function and will perform string interpolation on its arguments giving the same result.
identity could be replaced with any other suitable R function.
It replaces $x (where x can be any name consisting only of letters and numbers) in the string with the contents of the variable named x.
library(gsubfn) script2 <- "select * from $x WHERE DATE($y)> DATE_SUB(NOW(), INTERVAL 1 DAY)" fn$identity(script2)
3) sprintf It is also possible to do this without any packages just using
sprintf("select * from %s WHERE DATE(%s)> DATE_SUB(NOW(), INTERVAL 1 DAY)", x, y)