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

后端 未结 3 1040
攒了一身酷
攒了一身酷 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 05:52

    One hack, probably not the best way:

    fun2 <- function (var_pass) { fun1 (deparse(substitute(var_pass))) }
    
    fun1 <- function (some_variable) {(some_variable))}
    
    fun2(my_var)
    # "my_var"
    

    And you could run get on that. But as Paul H, suggests, there are better ways to track variables.

    0 讨论(0)
  • 2020-12-03 06:02

    I suggest you consider passing optional name value to these functions. I say this because it seems like you really want to use the name as a label for something in the end result; so it's not really the variable itself that matters so much as its name. You could do

    fun1 <- function (some_variable, name=deparse(substitute(some_variable))) {
        name
    }
    fun2 <- function (var_pass, name=deparse(substitute(var_pass))) { 
        fun1 (var_pass, name) 
    }
    my_var <- c(1,2)
    
    fun2(my_var)
    # [1] "my_var"
    
    fun1(my_var)
    # [1] "my_var"
    

    This way if you end up having some odd variable name and what to give a better name to a result, you at least have the option. And by default it should do what you want without having to require the name parameter.

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题