如何将变量(对象)名称转换为String

时间:2013-01-29 07:05:02

标签: string r object

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

 > foo <-c(3,4);

我想要做的是将"foo"转换为字符串。这样在一个函数中 我不必重新创建另一个额外的变量:

   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 ()
   }

1 个答案:

答案 0 :(得分:193)

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

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

myfunc(foo)
[1] "foo"
相关问题