[R] premature evaluation of symbols. Is that the way to describe this problem?

peter dalgaard PDalgd at gmail.com
Thu Apr 10 17:36:17 CEST 2014

On 10 Apr 2014, at 16:59 , Paul Johnson <pauljohn32 at gmail.com> wrote:

> I've been reading quite a while on this question, testing lots of
> ideas. The quote function seems to work, but I worry about how the R
> runtime environment finds all the pieces if I pass them through this
> way.
> mySpecialFeature <- function(model, ci){
>    pargs <- list(quote(model), quote(model.frame(model)[1:3, ]))
>    res <- do.call(myRegFit, pargs)
>    print(res)
> }
> See, that fixes it:
>> mySpecialFeature (m1)
> debugging in: (function (model, nd)
> predict(model, nd))(model, model.frame(model)[1:3, ])
> debug: predict(model, nd)
> Browse[2]> c
> exiting from: (function (model, nd)
> predict(model, nd))(model, model.frame(model)[1:3, ])
>          1           2           3
> -0.04755431  0.35162844 -0.11715522
> Are there dangers in this I don't know about?

I don't think so. A similar issue made the rounds quite recently over on R-devel ("Problem with do.call()") and the quote() trick was mentioned as a fix. 

The only potential issue that I can see is the fairly obvious one that the quoted arguments are evaluated later than unquoted ones, and possibly not at all due to lazy evaluation. This is of course largely the point of the maneuver, but there is also a slight risk that the object could change or disappear due to side effects when evaluating other arguments. This isn't different from the usual perils of lazy evaluation, though.


Peter Dalgaard, Professor
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com

More information about the R-help mailing list