[Rd] Lazy-evaluate elements wrapped with invisible

Gabriel Becker g@bembecker @end|ng |rom gm@||@com
Fri Oct 28 23:10:51 CEST 2022


Hi Dipterix,


On Fri, Oct 28, 2022 at 1:10 PM Dipterix Wang <dipterix.wang using gmail.com>
wrote:

> Hi,
>
> I was wondering if it is a good idea to delay the evaluation of expression
> within invisible(), just like data()/delayedAssign()?
>
> The idea is a function might return an invisible object. This object might
> not be used by the users if the function returns are not assigned nor
> passed to another function call. For example,
>
> f <- function() {
>   # do something eagerly
>
>   return(invisible({
>     # calculate message that might take long/extra memory, but only useful
> if printed out
>   }))
> }
>
> If `f()` is not immediately assigned to a variable, then there is no
> reason to evaluate invisible(…).
>

This is not quite true. The value, even when invisible, is captured by
.Last.value, and

> f <- function() invisible(5)

> f()

> .Last.value

[1] 5


Now that doesn't actually preclude what you're suggesting (just have to
wait for .Last.value to be populated by something else), but it does
complicate it to the extent that I'm not sure the benefit we'd get would be
worth it.

Also, in the case you're describing, you'd be pushing the computational
cost into printing, which, imo, is not where it should live. Printing a
values generally speaking, should just print things, imo.

That said, if you really wanted to do this, you could approach the behavior
you want, I believe (but again, I think this is a bad idea) by returning a
custom class that wraps formula (or, I imagine, tidyverse style quosures)
that reach back into the call frame you return them from, and evaluating
them only on demand.

Best,
~G


> This idea is somewhere between `delayedAssign` and eager evaluation. Maybe
> we could call it delayedInvisible()?
>
> Best,
> - Zhengjia
>
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

	[[alternative HTML version deleted]]



More information about the R-devel mailing list