如何将变量(对象)名转换为String [duplicate]

时间:2021-12-04 05:43:01

This question already has an answer here:

这个问题已经有了答案:

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

我有下面的数据框,变量名为foo;

 > foo <-c(3,4);

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:

我要做的是把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 个解决方案

#1


156  

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

您可以使用deparse和replace来获得函数参数的名称:

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

myfunc(foo)
[1] "foo"

#1


156  

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

您可以使用deparse和replace来获得函数参数的名称:

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

myfunc(foo)
[1] "foo"