I'm building a function where the main argument can be various things, e.g. a formula, a function or a lambda-function and it has to handle things accordingly.
library(rlang)
my_func=function(x){
if(is_function(x))
return("X is a function")
else if(is_lambda(x))
return("X is a lambda")
else if(is_formula(x))
return("X is a formula")
else
return("X is something else")
}
my_func(x=is.numeric) #function
my_func(x=A~B) #formula
my_func(x=~is.numeric(.x)) #formula too :'(
my_func(x="foo") #something else
As you can see, rlang::is_lambda did not recognize the lambda-function. In the help file example, they use as_function first (is_lambda(as_function(x))), but this throws a conversion error when x is not a lambda-function-formula. I'd rather not use trycatch if possible, as I find it sometimes leads to unreadable code and hidden bugs.
How can I test this case simply?