简体   繁体   English

如何将对象名称转换为字符对象

[英]How can I convert an object name to a character object

I have a function, that just do a print, mytable is a global variable 我有一个函数,只是打印, mytable是一个全局变量

f <- function(field){ print(mytable[mytable$a == field,]) }

My problem is that this function is called (not by me, it is sent as command to R by another process like an editor) as f(EXAMPLE) , f(EXAMPLE2) ... this fails because EXAMPLE is not a defined variable, I would like EXAMPLE to be "EXAMPLE" (a string). 我的问题是此函数被调用(不是我本人,它是由另一个进程(如编辑器)作为命令发送给R的),分别为f(EXAMPLE)f(EXAMPLE2) ……这失败了,因为EXAMPLE不是定义的变量,我希望EXAMPLE"EXAMPLE" (字符串)。

What can I do in the function f for field to have the value "EXAMPLE" when f(EXAMPLE) is evaluated ? f(EXAMPLE)时,对于field f具有值为“ EXAMPLE”的值,该怎么办?

I realize that this is very convoluted... 我意识到这很令人费解...

You can use deparse(substitute(yourobject)) : 您可以使用deparse(substitute(yourobject))

R> deparse(substitute(foo))
[1] "foo"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM