[R] Evaluating lazily 'f<-' ?

Leonard Mada |eo@m@d@ @end|ng |rom @yon|c@eu
Mon Sep 13 15:38:00 CEST 2021


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;


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