I am reading the http://www.cran.r-project.org/doc/manuals/R-lang.pdf manual chapter 4.3 and I just don't get it. Maybe someone can give me a quick explanation why R behaves in the following way.
fCall <- function(i){
dtData[i]
}
fSubstituteCall <- function(i){
iSub <- substitute(i)
dtData[eval(iSub)]
}
library(data.table)
dtData <- data.table(id=LETTERS, value=1:26)
dtData[id == 'C'] #works
fCall(id == 'C') #Error in eval(expr, envir, enclos) : object 'id' not found
fSubstituteCall(id == 'C') #works
Why does fSubstituteCall work and fCall not? Does it have to do with the evaluation of i? Or is it actually something specific to data.table package?
EDIT:
Thank you so far for your answers. I kind of get it and I agree that it is a duplicate of stackoverflow.com/q/14837902/602276. So I am going to simplify my question.
How do I make fPrintArgument print the argument i as a string? So in the case fCall('C') it should print out the string 'C', and in the fCall(id == 'C') it should print out the string 'id == "C"'.
Is this possible?
fPrintArgument <- function(i){
#This is what i have come up with so far, but it doesn't work
print(deparse(substitute(i)))
print(deparse((i)))
}
fCall <- function(x){
fPrintArgument(x)
}
fCall('C')
fCall(id == 'C')