Skip to content
Back to formatted view

Raw Message

Message-ID: <52ED34E6.30205@gmail.com>
Date: 2014-02-01T17:54:46Z
From: Duncan Murdoch
Subject: Avoid using "eval" in a neat way
In-Reply-To: <CACGxcfQkF7HH-RCeGVh=49+xzJ_1RFH8Q-igjHLzsQ3D1q-waw@mail.gmail.com>

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