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. [[alternative HTML version deleted]]
This has been discussed extensively, on this list as well as elsewhere. I suggest doing a web search, read up on the issue, and post back here only if you have specific questions that are not answered already. Best, Ista On Sat, Feb 1, 2014 at 11:40 AM, Hai Qian <hqian at gopivotal.com> 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. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
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
You have set up your criteria for success to be that your user has full freedom to specify code in strings to evaluate. Then you ask how to achieve this goal without evaluating that code. Are you thinking objectively at all about your question? The advice to not use eval has a number of justifications that you should be able to find yourself online (unnecessary obfuscation and program security are two). Implementing an interpreter is not where this advice applies. At the core of this problem, as long as you accept that your user is working within the R interpreter then they can provide your code with functions that access data on their own. You don't need to assume so much responsibility as your question assumes you have to. "Doctor, my head hurts!" "Then stop banging it against the wall." --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. On February 1, 2014 8:40:47 AM PST, Hai Qian <hqian at gopivotal.com> 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. > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.
On Sat, Feb 1, 2014 at 11:40 AM, Hai Qian <hqian@gopivotal.com> 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. > >Since the string is arbitrary R code ultimately it will have to be pushed through the R interpreter but we can avoid the superficial act of calling 'eval' by punting to some other function which in turn invokes 'eval' like this: func2 <- function(dat, s) do.call("with", list(dat, parse(text = s))) 'func2' can be run like this:> func2(dat, "x*2+y")[1] 4 7> func2(dat, "sin(x)*cos(y)")[1] -0.3501755 -0.9001976 Note that 'with' dispatches to 'with.default' which in turn invokes `eval`:> with.defaultfunction (data, expr, ...) eval(substitute(expr), data, enclos = parent.frame()) <bytecode: 0x0000000005db6790> <environment: namespace:base> [[alternative HTML version deleted]]