Skip to content
Prev 389130 / 398506 Next

Evaluating lazily 'f<-' ?

I try to clarify the code:


###
right = function(x, val) {print("Right");};
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)); # anything

### Does NOT work as expected
'right<-' = function(x, value) {
 ??? print("This line should be the first printed!")
 ??? print("But ERROR: x was already evaluated, which printed \"Padding\"");
 ??? x = substitute(x); # x was already evaluated before substitute();
 ??? return("Nothing"); # do not now what the behaviour should be?
}

right(padding(df)) = 1;

### Output:

[1] "Padding"
[1] "This line should be the first printed!"
[1] "But ERROR: x was already evaluated, which printed \"Padding\""
[1] "Padding = " # How did this happen ???


### Problems:

1.) substitute(x): did not capture the expression;
- the first parameter of 'right<-' was already evaluated, which is not 
the case with '%f%';
Can I avoid evaluating this parameter?
How can I avoid to evaluate it and capture the expression: "right(...)"?


2.) Unexpected
'padding<-' was also called!
I did not know this. Is it feature or bug?
R 4.0.4


Sincerely,


Leonard
On 9/13/2021 4:45 PM, Duncan Murdoch wrote: