R-score of function argument

I read http://www.cran.r-project.org/doc/manuals/R-lang.pdf chapter 4.3, and I just don't understand. Maybe someone can give me a brief explanation of why R behaves as follows.

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? Is this related to i? Or is it really something specific for the data.table package?

EDIT:

Thank you for your answers. I kind of understand, and I agree that this is a duplicate of stackoverflow.com/q/14837902/602276. Therefore, I am going to simplify my question.

How to make fPrintArgument print argument i as a string? Therefore, in the case of fCall ('C'), it should print the string 'C', and in fCall (id == 'C') it should print the string 'id ==' C ".

Is it 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') 
+4
source share
1 answer

Is this what you are looking for?

 fPrintArgument <- function(i) { cc <- sys.call(sys.parent(1)) print(deparse(cc[[2]])) } fCall <- function(x){ fPrintArgument(x) } fCall('C') fCall(id == 'C') 
0
source

Source: https://habr.com/ru/post/1499369/


All Articles