[Rd] [External] anonymous functions

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Tue Dec 8 14:58:54 CET 2020


On 08/12/2020 6:21 a.m., Duncan Murdoch wrote:
> On 07/12/2020 12:26 p.m., luke-tierney using uiowa.edu wrote:
>> I don't disagree in principle, but the reality is users want shortcuts
>> and as a result various packages, in particular tidyverse, have been
>> providing them. Mostly based on formulas, mostly with significant
>> issues since formulas weren't designed for this, and mostly
>> incompatible (tidyverse ones are compatible within tidyverse but not
>> with others). And of course none work in sapply or lapply. Providing a
>> shorthand in base may help to improve this. You don't have to use it
>> if you don't want to, and you can establish coding standards that
>> disallow it if you like.
> Here's a suggestion to let people define their own shorthands and work
> with the current |> definition.
> 
> Define "as.call" as an S3 generic, with a default definition something like >
> as.call.default <- function(x, f, ...) f(x, ...)

That's probably a bad choice of name for the generic in that it doesn't 
convert x to a call and it's only useful in a pipe, but I do think the 
idea of some generic function would be useful.

Duncan Murdoch

> 
> so one could use
> 
>    x |> as.call(mean)
> 
> to get the same result as
> 
>    x |> mean()
> 
> or (if working in the tidyverse)
> 
>    x |> as.call(~ .x + 1)
> 
> to use a method to be provided by rlang or purrr to convert their
> shorthand into a call that the pipe can work with.
> 
> We already have the generic as.function, which could be used internally
> by lapply and sapply for the same sort of purpose.  tidyverse has
> rlang::as_function, so they could pretty easily add methods for
> as.function if they wanted to allow people to use their shorthand in
> *apply functions.
> 
> Duncan Murdoch
>



More information about the R-devel mailing list