[Rd] bquote/evalq behavior changed in R-3.2.1

William Dunlap
Wed, 15 Jul 2015 21:05:25 -0000

You could test for the version of R when using rapply.
  > getRversion() >= "3.2.0"
  [1] TRUE

I rarely use rapply().  I often find that writing my own
purpose-built recursive function is easier than fitting
my problem into rapply's framework.


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Wed, Jul 15, 2015 at 1:46 PM, Dayne Filer <dayne.filer at gmail.com>
wrote:

> Bill,
>
> Is your conclusion to just update the code and enforce using the most
> recent version of R?
>
> Dayne
>
> On Wed, Jul 15, 2015 at 4:44 PM, Dayne Filer <dayne.filer at
gmail.com>
> wrote:
>
>> David,
>>
>> If you are referring to the solution that would be:
>>
>> rapply(list(test), eval, envir = fenv)
>>
>> I thought I explained in the question that the above code does not
work.
>> It does not throw an error, but the behavior is no different (at least
in
>> the output or result). Using the above code still results in the x
object
>> not being stored in fenv on 3.1.2.
>>
>> Dayne
>>
>> On Wed, Jul 15, 2015 at 4:40 PM, William Dunlap <wdunlap at
tibco.com>
>> wrote:
>>
>>> Another aspect of the change is (using TERR's RinR package):
>>> > options(REvaluators=list(makeREvaluator("R-3.1.3"),
>>>
makeREvaluator("R-3.2.0")))
>>> >
RCompare(rapply(list(quote(function(x)x),list(quote(pi),quote(7-4))),
>>> function(arg)typeof(arg)))
>>>      R version 3.1.3 (2015-03-09) R version 3.2.0 (2015-04-16)
>>> [1,] [1] "closure" "double"       [1]
"language" "symbol"
>>> [2,] [3] "double"                 [3]
"language"
>>>   $all.equal
>>>   $all.equal$`R version 3.1.3 (2015-03-09) vs. R version 3.2.0
>>> (2015-04-16)`
>>>   [1] "3 string mismatches"
>>>
>>> I prefer the new semantics, but it is a change.
>>>
>>>
>>>
>>> Bill Dunlap
>>> TIBCO Software
>>> wdunlap tibco.com
>>>
>>> On Wed, Jul 15, 2015 at 1:25 PM, David Winsemius <dwinsemius at
comcast.net
>>> > wrote:
>>>
>>>>
>>>> On Jul 15, 2015, at 12:51 PM, William Dunlap wrote:
>>>>
>>>> > I think rapply() was changed to act like lapply() in this
respect.
>>>> >
>>>>
>>>> When I looked at the source of the difference, it was that
typeof()
>>>> returned 'language' in 3.2.1, while it returned
'list' in the earlier
>>>> version of R. The first check in rapply's code in both
version was:
>>>>
>>>>  if (typeof(object) != "list")
>>>>         stop("'object' must be a list")
>>>>
>>>> Wrapping list() around the first argument and switching to
using eval
>>>> with an expression-object rather than a call-object seemed to
solve the
>>>> problem when this was posed as a question on StackOverflow, but
Dayne was
>>>> not happy with that solution for other reasons that he is not
describing.
>>>>
>>>> --
>>>> David.
>>>>
>>>> > In R-3.1.3 we got
>>>> > rapply(list(quote(1+myNumber)), evalq,
>>>> envir=list2env(list(myNumber=17)))
>>>> > #[1] 18
>>>> > rapply(list(quote(1+myNumber)), eval,
>>>> envir=list2env(list(myNumber=17)))
>>>> > #Error in (function (expr, envir = parent.frame(), enclos
= if
>>>> > (is.list(envir) ||  :
>>>> >  object 'myNumber' not found
>>>> > lapply(list(quote(1+myNumber)), evalq,
>>>> envir=list2env(list(myNumber=17)))
>>>> > #Error in eval(substitute(expr), envir, enclos) : object
'X' not found
>>>> > lapply(list(quote(1+myNumber)), eval,
>>>> envir=list2env(list(myNumber=17)))
>>>> > #[[1]]
>>>> > #[1] 18
>>>> > while in R-3.2.0 we get
>>>> > rapply(list(quote(1+myNumber)), evalq,
>>>> envir=list2env(list(myNumber=17)))
>>>> > #Error in eval(substitute(expr), envir, enclos) : object
'X' not found
>>>> > rapply(list(quote(1+myNumber)), eval,
>>>> envir=list2env(list(myNumber=17)))
>>>> > #[1] 18
>>>> > lapply(list(quote(1+myNumber)), evalq,
>>>> envir=list2env(list(myNumber=17)))
>>>> > #Error in eval(substitute(expr), envir, enclos) : object
'X' not found
>>>> > lapply(list(quote(1+myNumber)), eval,
>>>> envir=list2env(list(myNumber=17)))
>>>> > #[[1]]
>>>> > #[1] 18
>>>> >
>>>> > Make the FUN argument function(arg)sys.call() to see some
details of
>>>> the
>>>> > change.
>>>> >
>>>> >
>>>> > Bill Dunlap
>>>> > TIBCO Software
>>>> > wdunlap tibco.com
>>>> >
>>>> > On Wed, Jul 15, 2015 at 12:35 PM, Dayne Filer
<dayne.filer at gmail.com>
>>>> wrote:
>>>> >
>>>> >> In 3.1.2 eval does not store the result of the
bquote-generated call
>>>> in
>>>> >> the given environment. Interestingly, in 3.2.1 eval
does store the
>>>> result
>>>> >> of the bquote-generated call in the given environment.
>>>> >>
>>>> >> In other words if I run the given example with eval
rather than
>>>> evalq, on
>>>> >> 3.1.2 "x" is never stored in
"fenv," but it is when I run the same
>>>> code on
>>>> >> 3.2.1. However, the given example stores "x"
in "fenv" on 3.1.2, but
>>>> throws
>>>> >> the error I gave when run on 3.2.1.
>>>> >>
>>>> >> To give credit, I received the idea for using evalq
from SO:
>>>> >> http://stackoverflow.com/a/22559385
>>>> >>
>>>> >> Dayne
>>>> >>
>>>> >>
>>>> >> On Wed, Jul 15, 2015 at 3:29 PM, William Dunlap
<wdunlap at tibco.com>
>>>> wrote:
>>>> >>
>>>> >>> I am curious why you used evalq instead of eval in
this code.
>>>> >>>
>>>> >>> Bill Dunlap
>>>> >>> TIBCO Software
>>>> >>> wdunlap tibco.com
>>>> >>>
>>>> >>> On Wed, Jul 15, 2015 at 11:49 AM, Dayne Filer <
>>>> dayne.filer at gmail.com>
>>>> >>> wrote:
>>>> >>>
>>>> >>>> Hello,
>>>> >>>>
>>>> >>>> I upgraded from 3.1.2 to 3.2.1 and am
receiving errors on code that
>>>> >>>> worked
>>>> >>>> as I intended previously. Briefly, I am using
bquote to generate
>>>> >>>> expressions to modify data.table objects
within a  function, so I
>>>> need
>>>> >>>> the
>>>> >>>> changes to actually be stored in the given
environment.
>>>> Previously, I
>>>> >>>> used
>>>> >>>> code like the following:
>>>> >>>>
>>>> >>>> test <- list(bquote(x <- 10))
>>>> >>>> fenv <- environment()
>>>> >>>> rapply(test, evalq, envir = fenv)
>>>> >>>>
>>>> >>>> Although the code in the example above is much
simpler, it shows
>>>> the
>>>> >>>> problem. On 3.1.2 the expression is evaluated
and x is stored as
>>>> 10 in
>>>> >>>> the
>>>> >>>> given environment, fenv. In 3.2.1 the code
throws an error:
>>>> >>>>
>>>> >>>> Error in eval(substitute(expr), envir, enclos)
: object 'X' not
>>>> found
>>>> >>>>
>>>> >>>> I could not find anything in the release notes
that would explain
>>>> this
>>>> >>>> change. Changing evalq to eval works in 3.2.1,
but eval does not
>>>> store x
>>>> >>>> in
>>>> >>>> the given environment in 3.1.2.
>>>> >>>>
>>>> >>>> Thanks,
>>>> >>>>
>>>> >>>> Dayne
>>>> >>>>
>>>> >>>>        [[alternative HTML version deleted]]
>>>> >>>>
>>>> >>>> ______________________________________________
>>>> >>>> R-devel at r-project.org mailing list
>>>> >>>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>>> >>>>
>>>> >>>
>>>> >>>
>>>> >>
>>>> >
>>>> >       [[alternative HTML version deleted]]
>>>> >
>>>> > ______________________________________________
>>>> > R-devel at r-project.org mailing list
>>>> > https://stat.ethz.ch/mailman/listinfo/r-devel
>>>>
>>>> David Winsemius
>>>> Alameda, CA, USA
>>>>
>>>>
>>>