如何将变量(对象)名称转换为字符串

如何将变量(对象)名称转换为字符串

问题描述:

我有以下数据框,变量名"foo";

I have the following data frame with variable name "foo";

 > foo <-c(3,4);

我想做的是将 "foo" 转换成字符串.所以在一个函数中我不必重新创建另一个额外的变量:

What I want to do is to convert "foo" into a string. So that in a function I don't have to recreate another extra variables:

   output <- myfunc(foo)
   myfunc <- function(v1) {
     # do something with v1
     # so that it prints "FOO" when 
     # this function is called 
     #
     # instead of the values (3,4)
     return ()
   }

您可以使用 deparsesubstitute 来获取函数参数的名称:

You can use deparse and substitute to get the name of a function argument:

myfunc <- function(v1) {
  deparse(substitute(v1))
}

myfunc(foo)
[1] "foo"