简体   繁体   中英

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

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).

What can I do in the function f for field to have the value "EXAMPLE" when f(EXAMPLE) is evaluated ?

I realize that this is very convoluted...

You can use deparse(substitute(yourobject)) :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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