Skip to content
Prev 389233 / 398506 Next

Evaluating lazily 'f<-' ?

In your case, yes, there is a negative impact to formatting the code like:

x <- `padding<-`(right(x), ...)

and it comes from using 'substitute' without specifying an environment / /
list. It's my biggest problem with the tidyverse packages, the use of
non-standard evaluation. 'substitute' was originally created for simple
things like getting informative labels for data sets and plots, as well as
for 'delayedAssign'. For example, you could write your function to use the
above standard syntax, something like:

`padding<-` <- function (x, ..., value)
{
    sx <- substitute(x)
    choices <- c("bottom", "left", "top", "right")
    if (!is.call(sx) || !is.symbol(sx[[1L]]) ||
        !(k <- match(as.character(sx[[1L]]), choices, nomatch = 0L)))
        stop(gettextf("invalid 'x', must be a call to %s",
            paste(sQuote(choices), collapse = ", ")))
    choice <- choices[[k]]
    if (length(sx) != 2L)
        stop(gettextf("invalid 'x', %d arguments passed to '%s' which
requires 1",
            length(sx) - 1L, choice))
    x <- eval(sx[[2L]], parent.frame())
    # do whatever else with x, choice, ..., value
}

but given that you cannot use it like

padding(right(x)) <- 5

I can't say that I recommend it. Additionally, even if you define
`padding<-` like this, because of the non-standard evaluation from
'substitute', it means you CAN'T make a wrapper function for `padding<-`:

wrapper <- function (x, ..., value)
{
    # not sure exactly what this wrapper should do,
    # we'll add another element to 'value'
    `padding<-`(x = x, ..., value = c(list(value), "test"))
}

this won't work for a case like:

wrapper(right(letters))

because now, when you do 'substitute', it will give you the literal symbol
'x', not really what you wanted, because of the non-standard evaluation. Of
course, do whatever you want to make the syntax look the way you want, but
I think you're going about this the wrong way. I think the 'which' argument
I previously suggested will serve you far better. I hope this helps.
On Wed, Sep 15, 2021 at 2:26 AM Leonard Mada <leo.mada at syonic.eu> wrote: