I have a function, defined as follows
fn1 <- function(var = NULL) {
if (missing(var)) var else deparse(substitute(var))
}
I can call this function and it gives me what I want.
fn1()
# NULL
fn1(test)
# [1] "test"
I now want to functionalise the deparsing of var.
fn2 <- function(var = NULL) {
deparse_var(var)
}
deparse_var <- function(var) {
if (missing(var)) var else deparse(substitute(var))
}
But this does not give me the intended result
fn2()
# [1] "var"
fn2(test)
# [1] "var"
Since I have the value of var inside deparse_var(), I can check whether or not it is NULL. But the deparse does not work if it isn't.
deparse_var <- function(var) {
if (is.null(var)) var else deparse(substitute(var))
}
fn2()
# [1] NULL
fn2(test)
# Error in deparse_var(var) : object 'test' not found