如何将变量(对象)名称转换为字符串 [英] How to convert variable (object) name into String

查看:70
本文介绍了如何将变量(对象)名称转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据框,变量名"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"

这篇关于如何将变量(对象)名称转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆