[R] Evaluating lazily 'f<-' ?

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Mon Sep 13 15:45:12 CEST 2021


On 13/09/2021 9:38 a.m., Leonard Mada wrote:
> Hello,
> 
> 
> I can include code for "padding<-"as well, but the error is before that,
> namely in 'right<-':
> 
> right = function(x, val) {print("Right");};
> # more options:
> padding = function(x, right, left, top, bottom) {print("Padding");};
> 'padding<-' = function(x, ...) {print("Padding = ");};
> df = data.frame(x=1:5, y = sample(1:5, 5));
> 
> 
> ### Does NOT work
> 'right<-' = function(x, val) {
>         print("Already evaluated and also does not use 'val'");
>         x = substitute(x); # x was evaluated before
> }
> 
> right(padding(df)) = 1;

It "works" (i.e. doesn't generate an error) for me, when I correct your 
typo:  the second argument to `right<-` should be `value`, not `val`.

I'm still not clear whether it does what you want with that fix, because 
I don't really understand what you want.

Duncan Murdoch

> 
> 
> I want to capture the assignment event inside "right<-" and then call
> the function padding() properly.
> 
> I haven't thought yet if I should use:
> 
> padding(x, right, left, ... other parameters);
> 
> or
> 
> padding(x, parameter) <- value;
> 
> 
> It also depends if I can properly capture the unevaluated expression
> inside "right<-":
> 
> 'right<-' = function(x, val) {
> 
> # x is automatically evaluated when using 'f<-'!
> 
> # but not when implementing as '%f%' = function(x, y);
> 
> }
> 
> 
> Many thanks,
> 
> 
> Leonard
> 
> 
> On 9/13/2021 4:11 PM, Duncan Murdoch wrote:
>> On 12/09/2021 10:33 a.m., Leonard Mada via R-help wrote:
>>> How can I avoid evaluation?
>>>
>>> right = function(x, val) {print("Right");};
>>> padding = function(x) {print("Padding");};
>>> df = data.frame(x=1:5, y = sample(1:5, 5));
>>>
>>> ### OK
>>> '%=%' = function(x, val) {
>>>        x = substitute(x);
>>> }
>>> right(padding(df)) %=% 1; # but ugly
>>>
>>> ### Does NOT work
>>> 'right<-' = function(x, val) {
>>>        print("Already evaluated and also does not use 'val'");
>>>        x = substitute(x); # is evaluated before
>>> }
>>>
>>> right(padding(df)) = 1
>>
>> That doesn't make sense.  You don't have a `padding<-` function, and
>> yet you are trying to call right<- to assign something to padding(df).
>>
>> I'm not sure about your real intention, but assignment functions by
>> their nature need to evaluate the thing they are assigning to, since
>> they are designed to modify objects, not create new ones.
>>
>> To create a new object, just use regular assignment.
>>
>> Duncan Murdoch



More information about the R-help mailing list