waanders waanders -4 years ago 112
R Question

Dynamic "string" in R

Simple question, but cannot find the answer.

Instead of:

Df <- sqlQuery(ch, "SELECT * FROM tblTest WHERE Id=25")

I want a more dynamic piece of code. Something like:

Id <- 25
Df <- sqlQuery(ch, c("SELECT * FROM tblTest WHERE Id=", Id))

But this is not correct.

Answer Source

We can use paste:

Df <- sqlQuery(ch, paste("SELECT * FROM tblTest WHERE Id =", Id))

c concatenates into a vector, paste is for string concatenation.

Or we can use sprintf:

sprintf("SELECT * FROM tblTest WHERE Id = %s", Id)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download