[datatable-help] best way of eval-ing a list of quoted expressions (Frank Erickson)

Juan Manuel Truppia jmtruppia at gmail.com
Fri Jan 16 15:56:28 CET 2015


With a little more work you could keep the definitions with the names. Here
is how

dt <- data.table(a = runif(10))
ee <- quote(list(3 * a, a +2))
dt[, names(ee)[-1] := eval(ee)]

Sorry, hope this now sticks to the thread

On Fri Jan 16 2015 at 11:19:57 AM Frank Erickson <fperickson at wisc.edu>
wrote:

> Thanks! I keep forgetting that I can eval() a larger statement. I think
> I'll stick to an option that keeps the names close to the definitions for
> now, though.
>
> By the way, you replied to the r-forge forum "digest," outside of the
> original thread. I might've missed the reply if there weren't so few active
> conversations.
>
> On Thu, Jan 15, 2015 at 11:07 AM, Juan Manuel Truppia <jmtruppia at gmail.com
> > wrote:
>
>> I don't know what you are trying to achieve, but I usually quote the
>> list, instead of generating a list of quotes. I think that your issue is
>> similar to something I've faced in the past, and I usually solve it like
>> this
>>
>> dt <- data.table(a = runif(10))
>> ee <- quote(list(3 * a, a +2))
>> dt[, c("b", "c") := eval(ee)]
>>
>> I still don't know how to define the column names in the quoted
>> expression, instead of in the `:=` call.
>>
>> Hope it helps
>>
>>
>> On Thu Jan 15 2015 at 8:00:10 AM <
>> datatable-help-request at lists.r-forge.r-project.org> wrote:
>>
>>> Send datatable-help mailing list submissions to
>>>         datatable-help at lists.r-forge.r-project.org
>>>
>>> To subscribe or unsubscribe via the World Wide Web, visit
>>>         https://lists.r-forge.r-project.org/cgi-bin/mailman/
>>> listinfo/datatable-help
>>>
>>> or, via email, send a message with subject or body 'help' to
>>>         datatable-help-request at lists.r-forge.r-project.org
>>>
>>> You can reach the person managing the list at
>>>         datatable-help-owner at lists.r-forge.r-project.org
>>>
>>> When replying, please edit your Subject line so it is more specific
>>> than "Re: Contents of datatable-help digest..."
>>>
>>>
>>> Today's Topics:
>>>
>>>    1. best way of eval-ing a list of quoted expressions (Frank Erickson)
>>>
>>>
>>> ----------------------------------------------------------------------
>>>
>>> Message: 1
>>> Date: Wed, 14 Jan 2015 17:06:36 -0500
>>> From: Frank Erickson <fperickson at wisc.edu>
>>> To: "data.table help" <datatable-help at lists.r-forge.r-project.org>
>>> Subject: [datatable-help] best way of eval-ing a list of quoted
>>>         expressions
>>> Message-ID:
>>>         <CAJd-hdmsub_h_oh2rGrBuvbGFCk4S9ZoTMi+
>>> FrfEssH9mqoLiA at mail.gmail.com>
>>> Content-Type: text/plain; charset="utf-8"
>>>
>>> Hi,
>>>
>>> I'm wondering what the most idiomatic or efficient approach is...? Here's
>>> my example:
>>>
>>> expr_nonlin = list(
>>>   early = quote(tt/TT*(tt/TT < .2)),
>>>   late  = quote(tt/TT*(tt/TT > .8))
>>> )
>>>
>>> # eval on a single expr works
>>> data.table(tt=1,TT=100)[,early:=eval(expr_nonlin$early)][]
>>>
>>> # lapply eval does not work
>>> data.table(tt=1,TT=100)[,names(expr_nonlin):=lapply(expr_nonlin,eval)][]
>>>
>>> # (1) envir fixes it
>>> DT <- data.table(tt=1,TT=100)
>>> DT[,names(expr_nonlin):=lapply(expr_nonlin,eval,envir=DT)][]
>>>
>>> # (2) or a for loop
>>> DT <- data.table(tt=1,TT=100)
>>> for (i in names(expr_nonlin)) DT[,(i):=eval(expr_nonlin[[i]])]
>>>
>>> (1) and (2) both work. Is either preferable?
>>>
>>> (1) calls [.data.table fewer times, but messes around with environments,
>>> which always seem fragile.
>>>
>>> ------------------
>>>
>>> One more quick question: In approach (1), is there a way to skip the
>>> names(zzz):= part? I see that this doesn't work:
>>>
>>> DT <- data.table(tt=1,TT=100)
>>> DT[,do.call(`:=`,lapply(expr_nonlin,eval,envir=DT))][]
>>>
>>>
>>> Thanks,
>>>
>>> Frank
>>> -------------- next part --------------
>>> An HTML attachment was scrubbed...
>>> URL: <http://lists.r-forge.r-project.org/pipermail/
>>> datatable-help/attachments/20150114/035b1682/attachment-0001.html>
>>>
>>> ------------------------------
>>>
>>> _______________________________________________
>>> datatable-help mailing list
>>> datatable-help at lists.r-forge.r-project.org
>>> https://lists.r-forge.r-project.org/cgi-bin/mailman/
>>> listinfo/datatable-help
>>>
>>> End of datatable-help Digest, Vol 59, Issue 2
>>> *********************************************
>>>
>>
>> _______________________________________________
>> datatable-help mailing list
>> datatable-help at lists.r-forge.r-project.org
>>
>> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20150116/58decfce/attachment.html>


More information about the datatable-help mailing list