Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting function calls to characters in R [duplicate]

Tags:

function

r

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?

like image 630
Indrajeet Patil Avatar asked Jul 15 '20 11:07

Indrajeet Patil


3 Answers

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"
like image 92
user1317221_G Avatar answered Nov 06 '22 10:11

user1317221_G


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.

like image 45
Cole Avatar answered Nov 06 '22 10:11

Cole


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"
like image 38
Matt Avatar answered Nov 06 '22 10:11

Matt