tidyverse / magrittr

Improve the readability of R code with the pipe

Home Page:https://magrittr.tidyverse.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`magrittr::freduce()` doesn't support lambda functions

emmansh opened this issue · comments

Why doesn't magrittr::freduce() support lambda functions?

library(magrittr)

my_3_operations_anon <- 
  list(
  function(x) mean(x),
  function(x) sqrt(x),
  function(x) log(x)
)


my_3_operations_lambda <- 
  list(
    ~mean(.),
    ~sqrt(.),
    ~log(.)
  )

magrittr::freduce(1:10, my_3_operations_anon)
#> [1] 0.852374
magrittr::freduce(1:10, my_3_operations_lambda)
#> Error in function_list[[i]](value): attempt to apply non-function

Created on 2021-10-26 by the reprex package (v2.0.1)

Now that base R also has syntax for lambda functions there's not as much benefit in supporting tidyverse lambdas.

Also magrittr currently has zero dependencies and we probably don't want to add one just for that. rlang is already in Suggests so we could check for a formula, check that rlang is installed, and then call rlang::as_function().

You're welcome to send a PR but there is no release planned in the immediate future. It might be years until that makes it to CRAN in the very worst case, at which point the transition to native lambdas will have made much progress.

@lionel- thanks. Could you demonstrate how you'd use rlang::as_function() in this example (i.e., assuming it's installed)?

oh if you just want to transform on your side then it's simply:

lapply(my_3_operations_lambda, rlang::as_function)

OK then, I'll live with that hack :)

Thanks.