[R] Avoid using "eval" in a neat way
Duncan Murdoch
murdoch.duncan at gmail.com
Sat Feb 1 18:54:46 CET 2014
On 14-02-01 11:40 AM, Hai Qian wrote:
> I know using eval is not optimal and maybe bad, but how to avoid using eval
> in the following example
>
> func1 <- function(dat, eval.this) {
> eval(parse(text = paste0("with(dat, ", eval.this, ")")))
> }
>
> dat <- data.frame(x = 1:2, y = 2:3)
>
> func1(dat, "x*2+y")
>
> func1(dat, "sin(x)*cos(y)")
>
> Here eval.this is a string that contains whatever the user wants to
> evaluate. I wonder whether there is a neat way to avoid using eval in this
> case? So far I have not figured out a way to do this.
You're asking how to parse some text and evaluate it without using eval.
Sounds impossible to me.
Duncan Murdoch
More information about the R-help
mailing list