This code
> A <- data.frame(col1 = c(1,2,3),col2 = c("red","blue","green"))
> color_num <- 2
> fn$sqldf("select * from A where col1 >= '$color_num'")
yields the error
Error in eval(parse(text = paste(..., sep = "")), env) : object 'color' not found
But if the variable color_num
is instead given a name with no underscore (say colornum
), then executing fn$sqldf("select * from A where col1 >= '$colornum'")
yields the expected results with no error.
I believe sqldf
is replacing underscores with periods behind the scenes, causing it to treat the component preceding the underscore as a table and the part following as a column name. This answer (and comments) to a question about column names in sqldf
indicates that the library at one time replaced dots with underscores but no longer does, but I couldn't find anything about underscores being replaced with dots.
This is an issue since the naming convention I'm using makes heavy use of underscores for variable names.
Is there any way to get variable names with underscores in them working in sqldf
queries?