Why is the "object" x "not found" in a string that does not use x?

I get this:

Error in paste0(width, on, k) : object 'x' not found 

I am completely confused why he complains about the "x" on this particular line!

on is a character, width and k are numbers. x exists (this is a parameter of this function). This line and the previous ones look like this:

 print(index(x)) stopifnot(length(index(x))>=1) #str(on);str(k);str(width) extra=list( paste0(width,on,k) ) 

But even stranger, when I added a commented line that he complains about:

 Error in str(on) : object 'x' not found 

What caused this question, there were several call levels, I added this line:

 rm(x) 

( rm(list=c("x")) gives the same behavior.)

So, I was expecting the error "x not found". But not on this line (even in this function)!

Background : I found an error in which the code relied on a global variable named x , which should be passed as a parameter. He worked in unit test, failed in real code, because the variable in question was not called "x" in real code !! So, I decided to explicitly delete each variable when I finished it, to find out if I have more errors of this type.

(If the above code snippets are not enough for someone to go “Yeah Darren, you still don't understand how R ... works,” I will try to create a minimal example to reproduce the problem.)

+7
source share
1 answer

This is because x used to create one of the parameters of the function call. The following is a minimal example:

 f=function(d,on){ print(on) } #AAA x=1:4 attr(x,'extra')=list(a=1,b="xxx") d=mean(x) rm(x) #Not needed any more f(d, attr(x,'extra') ) # BBB 

It gives an error for the #AAA string (see below) instead of #BBB, as you would expect.

This is because attr is a primitive function. See http://cran.r-project.org/doc/manuals/r-release/R-lang.html#Builtin-objects-and-special-forms

(And I finally found confirmation that primitive functions work the same as promise objects, i.e. doing a delay: http://cran.r-project.org/doc/manuals/R-ints.html#Argument -evaluation )


The error I am getting is as follows:

 Error in print(on) : object 'x' not found 6: print(on) at dummy.R#1 5: f(d, (attr(x, "extra"))) at dummy.R#9 4: eval(expr, envir, enclos) 3: eval(ei, envir) 2: withVisible(eval(ei, envir)) 1: source("dummy.R") 

those. it complains about x that does not exist on line 1, and not on line 9. You can make the function f much larger and not use on until much deeper in the function, even pass it to another function, and the error will trigger until until on is evaluated. For example. this longer example:

 g=function(x,on){ cat("Something else:",x,"\n") print(on) } f=function(d,on){ cat("Do something:",d,"\n") g(d,on) } x=1:4 attr(x,'extra')=list(a=1,b="xxx") d=mean(x) rm(x) #Not needed any more f(d, (attr(x,'extra')) ) 

To liven up this example, I added the x parameter to g() . This reflects the code in the original question, and it's easy to see how this can be confusing: "I have x, but he says he can't see it!". To paraphrase Obi-Wan: "These are not the x you are looking for ..."

+4
source

All Articles