I want to use quoted arguments in my function and I would like to allow the user to specify that they don't want to use the argument by setting it to NULL
. However, rlang::ensym
throws an error when it receives a NULL
argument. Here is my code:
f <- function(var){
rlang::ensym(var)
return(var + 2)
}
# This works
variable = 2
f(variable)
# This throws an error
f(NULL)
The error message is:
Error: Only strings can be converted to symbols
I already tried adding an if-clause with is.null(var)
before the expression with rlang::ensym
, but of course, this doesn't work as the variable is not yet quoted at this time.
How can I check that the supplied quoted variable is NULL
in order to handle it differently?