[R] Evaluating lazily 'f<-' ?

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


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