如何在R(替代)中获取变量的名称?

时间:2020-12-02 19:37:30

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 function in R looked only in "local" environment, or just for one level up. Well, let me explain it by code:

我试图通过几个函数传递变量,并在最终函数上我希望得到原始变量的名称。但看起来R中的替代功能只能在“本地”环境中看到,或者只是向上一级。好吧,让我用代码解释一下:

fun1 <- function (some_variable) {deparse(substitute(some_variable)}
fun2 <- function (var_pass) { fun1 (var_pass) }
my_var <- c(1,2) # I want to get 'my_var' in the end
fun2 (my_var) # > "var_pass"

Well, it seems like we printing the name of variable that only pass to the fun1. Documentation of the substitute tells us, that we can use env argument, to specify where we can look. But by passing .Global or .BaseNamespaceEnv as an argument to substitute I got even more strange results - "some_variable"

好吧,看起来我们打印的变量名只传递给fun1。替代品的文档告诉我们,我们可以使用env参数来指定我们可以看到的位置。但是通过传递.Global或.BaseNamespaceEnv作为替代参数我得到了更奇怪的结果 - “some_variable”

I believe that answer is in this function with using env argument, so, could you please explain me how it works and how can I get what I need. Thanks in advance!

我相信使用env参数可以回答这个问题,所以,请你解释一下它是如何工作的,我怎样才能得到我需要的东西。提前致谢!

2 个解决方案

#1


9  

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.

这样,如果您最终得到一些奇怪的变量名称以及为结果提供更好的名称,那么您至少可以选择。默认情况下,它应该做你想要的,而不需要name参数。

#2


0  

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.

你可以继续这样做。但正如Paul H所说,有更好的方法来跟踪变量。

#1


9  

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.

这样,如果您最终得到一些奇怪的变量名称以及为结果提供更好的名称,那么您至少可以选择。默认情况下,它应该做你想要的,而不需要name参数。

#2


0  

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.

你可以继续这样做。但正如Paul H所说,有更好的方法来跟踪变量。