I have a much more complicated function but the minimal version of it boils down to the following where I need to convert the entered function by the user to a character. But I can't figure out how to do this (I've also tried a bunch of rlang
hacks that didn't work).
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'
How can I do this?
maybe this would work?
deparse(substitute(stats::lm))
[1] "stats::lm"
so using your function:
> foo <- function(.f) {
+ if (deparse(substitute(.f)) == "stats::lm") {
+ print("this is lm")
+ } else {
+ print("this is not lm")
+ }
+ }
>
> foo(stats::lm)
[1] "this is lm"
You can use identical
to compare the argument .f
with the function you are interested in:
foo <- function(.f) {
if (identical(.f, stats::lm)) {
print("this is lm")
} else {
print("this is not lm")
}
}
foo(stats::lm)
#> [1] "this is lm"
The issue with the original approach is that as.character(sum)
or any type of as.character(fx)
does not mean anything to R. You can use substitute(.f) == quote(stats::lm)
as well. Mainly, we need to intercept .f
before it is evaluated and returns a function.
Here's another dplyr
option using enquo
and as_label
:
foo <- function(.f) {
.f <- enquo(.f)
if (!!as_label(.f) == "stats::lm") {
print("this is lm")
} else {
print("this is not lm")
}
}
> foo(stats::lm)
[1] "this is lm"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With