[R-SIG-Mac] Scoping question

iuhz7j202 at sneakemail.com iuhz7j202 at sneakemail.com
Tue Apr 14 18:08:15 CEST 2009


Hi,

I'm struggling with a bit of scoping using the with() function. I think it's best explained with an example:
  x <- list(foobar=2)
  f <- function(){foobar*2}
  with(x,f())
This throws an error that 'foobar' does not exist. However if I define foobar in the global environment, f will find it whether I call it from the global environment or from within x. I have also tried using get("foobar",parent.frame()) within f, along with other variations, with no success.

I could always just add foobar as an argument to f, but in my actual work case foobar is a potentially very large object that is not worth making a copy of. (I don't know how much of an effect this will actually have on performance). I also just don't like all the bookkeeping of having to know which objects in x I am going to need in f and passing them all as arguments.

I would appreciate any insight about why the scoping is not working as I thought it would, and if there is any way to get around it without directly passing arguments to f.

Thank you,
Peter



More information about the R-SIG-Mac mailing list