我有一个更复杂的函数,但它的最小版本归结为以下,我需要将用户输入的函数转换为字符。但我无法弄清楚如何做到这一点(我也尝试了一堆rlang不起作用的黑客)。
foo <- function(.f) {
if (as.character(.f) == "stats::lm") {
print("this is lm")
} else {
print("this is not lm")
}
}
foo(stats::lm)
#> Error in as.character(.f): cannot coerce type 'closure' to vector of type 'character'
我怎样才能做到这一点?