How to get name of variable in R (substitute)?

后端 未结 3 1117
攒了一身酷
攒了一身酷 2020-12-03 05:42

I stacked with trying to pass variable through few functions, and on the final function I want to get the name of the original variable. But it seems like substitute functio

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-03 06:05

    Another approach I'd like to suggest is to use rlang::enexpr. The main advantage is that we don't need to carry the original variable name in a parameter. The downside is that we have to deal with expressions which are slightly trickier to use.

    > fun1 <- function (some_variable) {
        message("Entering fun1")
        rlang::enexpr(some_variable)
    }
    > fun2 <- function (var_pass) {
        message("Entering fun2")
        eval(parse(text=paste0("fun1(", rlang::enexpr(var_pass), ")")))
    }
    > my_var <- c(1, 2)
    > fun1(my_var)
    #Entering fun1
    my_var
    > fun2(my_var)
    #Entering fun2
    #Entering fun1
    my_var
    

    The trick here is that we have to evaluate the argument name in fun2 and build the call to fun1 as a character. If we were to simply call fun1 with enexpr(var_pass), we would loose the notion of fun2's variable name, because enexpr(var_pass) would never be evaluated in fun2:

    > bad_fun2 <- function (var_pass) {
        message("Entering bad fun2")
        fun1(rlang::enexpr(var_pass))
    }
    > bad_fun2(my_var)
    #Entering bad fun2
    #Entering fun1
    rlang::enexpr(var_pass)
    

    On top of that, note that neither fun1 nor fun2 return variable names as character vectors. The returned object is of class name (and can of course be coerced to character). The bright side is that you can use eval directly on it.

    > ret <- fun2(my_var)
    #Entering fun2
    #Entering fun1
    > as.character(ret)
    [1] "my_var"
    > class(ret)
    [1] "name"
    > eval(ret)
    [1] 1 2
    

提交回复
热议问题