2

I just updated to R-3.2.1, and am running into errors with code that ran previously on R-3.1.2 (based on this answer).

For example:

test <- list(bquote(x <- 10))
fenv <- environment()
rapply(test, evalq, envir = fenv)

The above code worked well before, but now gives the error:

Error in eval(substitute(expr), envir, enclos) : object 'X' not found

The expected behavior would be to define x in fenv. I am trying to figure out how to proceed, such that the code works on the current and previous version of R (if possible).

Interestingly, eval gives the expected behavior in R-3.2.1 where it did not in R-3.1.2.

EDIT BASED ON COMMENTS WITH @BondedDust

The suggested rapply(list(test), eval, envir = fenv) returns 10 in both versions but does not store x in fenv in R-3.1.2. The suggested code does, however, return 10 and store x in fenv in R-3.2.1, as desired.

The original suggestion by @BondedDust: rapply(list(test), evalq, envir = fenv) gave the same error in R-3.2.1 as the code above in the original question.

I still hope to get to a solution that stores the x variable in fenv in both old and new versions of R.

EDIT TO SHOW COMPLEXITY

I have to use bquote because I am generating the expressions from another function. This allows me to create complex calls to a data.table object without sending the data.table object to the function. Within the function, I can call a method, and then create the data.table expression and apply it to the data.table object without sending the data.table to an additional function. I have tried to simplify the problem below:

fenv <- environment()
func_list <- list(f1 = function(val) {e1 <- bquote(x <- .(val)); list(e1)})
test <- func_list[["f1"]](10)
rapply(test, evalq, envir = fenv)

The rapply is necessary, because there are functions in the "func_list" that return a list of expressions, not necessarily just one as in the above example. What I end up with is a nested list of expressions generated by bquote that I need to evaluate in a given environment, such that the resulting object(s) (or changes to a data.table) are stored in the given environment.

I had this working in previous versions of R, and I am trying to figure out why the behavior has changed and how to rectify the code such that it works both in old and new versions of R.

Community
  • 1
  • 1
dayne
  • 7,504
  • 6
  • 38
  • 56
  • And what is the expected behavior here? Are you trying to define the `x` value in the `fenv` environment? – MrFlick Jul 14 '15 at 20:29
  • @MrFlick Yes, sorry for the confusion. I am using code similar to the example to modify data.tables within a function such that I do not have to make any unnecessary copies. – dayne Jul 14 '15 at 20:31
  • I think the edit does not create what you say should be the test case, namely a nested list of "expressions" (especially since `bquote` actually returns 'call's rather than R 'expression's). We still do not have an example that has sufficient complexity for a tested solution. It's also unclear which environment the `val` object is supposed to reside in at the time of the creation of `e1`. – IRTFM Jul 15 '15 at 19:19
  • I think you'd be better off asking a question that is more closely tied to what you are trying to do, not the exact implementation you are currently using. I doubt that either `bquote()` or `rapply()` are the best tools here. – hadley Jul 20 '15 at 15:28

1 Answers1

1

The typeof function no longer returns "list" but instead returns "language" for the "test"-object. The obvious fix is:

  rapply(list(test), evalq, envir = fenv)
[1] 10

Which works in R 3.1.2, but need to use this in R 3.2.1 RC

 rapply(list(test), eval, envir = fenv)
[1] 10    

I also looked at the news() to see whether any of this might have been announced and could not find anything related to changes in typeof, rapply or evalq.

Edit: Tried both quote and expression as functions to create the "test" object. With expression-vector got the desired results on both 3.1.2 and 3.2.1RC

test <- expression( x <- 10 )
nenv <- new.env()
ls(envir=nenv)
#character(0)
 rapply(list(test), eval, envir = nenv)
#[1] 10
 ls(envir=nenv)
#[1] "x"
 nenv$x
#[1] 10
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • I really appreciate your continued help. Unfortunately, I have to use `bquote`, because I am generating the expressions based on variables. – dayne Jul 15 '15 at 15:50
  • If you want "continued help" then please post a problem example that actually matches the complexity of the problem. – IRTFM Jul 15 '15 at 16:01
  • I was just trying to thank you for all the work you have already done. I added an edit to the question. Please let me know if I am unclear. – dayne Jul 15 '15 at 18:37