[R] Help needed with eval parse
murdoch@dunc@n @end|ng |rom gm@||@com
Wed Sep 18 16:05:58 CEST 2019
On 18/09/2019 8:43 a.m., Huzefa Khalil wrote:
> Hello R-users,
> I have been running a script which produces objects based on the
> column names of a data.frame. The column names are of the form CB_1-1,
> CB_1-2, etc. Now this calculation was rather long and memory
> intensive, so I would rather not have to do it again after fixing the
> column names using "make.names". As a consequence, I am left with a
> bunch of R objects with `-` in the name.
> Accessing them is proving challenging and any help would be appreciated.
> Reproducible example:
> `cb_1-2` <- "hello world"
> t <- "cb_1-2"
> t <- as.name(t)
> t <- eval(parse(text = t))
> Error in eval(parse(text = t)) : object 'cb_1' not found
After t <- as.name(t), you already have language: no need to parse it
works. If you have more complicated expressions, use call() to set them
ụp. For example, call("paste0", t, "!") evaluates to
and evaluating that expression via
eval(call("paste0", t, "!"))
 "hello world!"
Don't go back and forth between language objects and text
representations of them, because it's hard to do that without
introducing changes. In other words, don't use eval(parse()).
More information about the R-help