Approximating non-standard evaluation like in R, e.g. for plot labels

jules
Mon, 22 Jun 2020 20:18:23 +0000
General Usage

Sometimes I miss R’s functionality where expressions passed into plotting functions are used as label names. The non-standard evaluation part of it I find pretty confusing actually, i.e. that the expressions are not run before being passed into the functions but after. But being aware of what the user typed to offer them a bit more convenience can be really nice.

I had an idea to approximate this behavior a while ago using a type and a macro:

struct CodeValue{V}
    code::Expr
    value::V
end

macro ~(e)
    qn = QuoteNode(e)
    :(CodeValue($qn, $(esc(e))))
end

So you could dispatch on the CodeValue type for plotting recipes, in Plots for example, and use the eagerly computed value as you normally would, but the expression for creating labels. Maybe there are other use cases besides this one?

It would look like this:

plot(@~ sin(1:10))

And then the plot could have the ylabel sin(1:10) automatically applied, unless overridden by the user.

I’m just on the fence if I find this useful enough or if the additional burden of typing @~ diminishes the usefulness. Maybe you have some ideas or thoughts about this, if you have used R before you’ll know what I mean.

16 posts - 7 participants