我正在尝试编写一个可以简单循环的函数deparse()
,substitute()
如下所示:
get_name <- function(x) {
deparse(substitute(x))
}
的要点get_name()
是返回函数参数的字符对象。get_name()
在我的全局环境中调用时,它正在工作,如下所示:
# no problem here, returns "Variable1" as expected
get_name(x = Variable1)
[1] "Variable1"
当我尝试使用另一个函数时,就会出现问题get_name()
。如下所示:
# a new function that uses get_name()
new_function <- function(arg1) {
get_name(x = arg1)
}
# returns "arg1"
# should be returning "Variable1"
new_function(arg1 = Variable1)
[1] "arg1"
我最初的想法是,一定有办法使用环境来解决这个问题。但是,当我尝试使用eval()
它时,它不起作用,因为Variable1
它实际上不是我全局环境中的对象,它只是我函数参数中的值。
我也尝试过其他函数,例如和rlang
的组合,但不幸的是,它不起作用。as_string()
ensym()