Hi all! I'm writing a function that takes some data x and a function aggr_func and I would like to return a list with the aggregated data & the function passed in aggr_func. rlang::expr_text() and rlang::expr(), but while this works outside of the function context it does not work with an argument! Here's a reprex:
library(rlang)
#> Warning: package 'rlang' was built under R version 3.5.3
# expected behavior
expr_text(expr(mean))
#> [1] "mean"
test_func <- function(x, aggr_func) {
list(
x = aggr_func(x),
aggr_func = expr_text(expr(aggr_func))
)
}
# returns name of argument ("aggr_func") instead of "mean"
test_func(1:10, mean)
#> $x
#> [1] 5.5
#>
#> $aggr_func
#> [1] "aggr_func"
Created on 2019-05-16 by the reprex package (v0.2.1)
Using rlang::!! doesn't help entirely--it returns what's getting applied to the data and not the text from the argument:
library(rlang)
#> Warning: package 'rlang' was built under R version 3.5.3
test_func <- function(x, aggr_func) {
list(
x = aggr_func(x),
aggr_func = expr_text(expr(!!aggr_func))
)
}
test_func(1:10, mean)
#> $x
#> [1] 5.5
#>
#> $aggr_func
#> [1] "function (x, ...) \nUseMethod(\"mean\")"
Created on 2019-05-16 by the reprex package (v0.2.1)