string 如何将变量(对象)名称转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14577412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:47:18 来源:igfitidea点击:
How to convert variable (object) name into String
提问by neversaint
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 ()
}
回答by Sven Hohenstein
You can use deparse
and substitute
to get the name of a function argument:
您可以使用deparse
和substitute
来获取函数参数的名称:
myfunc <- function(v1) {
deparse(substitute(v1))
}
myfunc(foo)
[1] "foo"