Dear R People: Hope you're having a nice day. Here is a character vector:> yz[1] "pexp(3.2,rate=1)"> str(yz)chr "pexp(3.2,rate=1)">And I would like to evaluate that vector. I tried:> eval(as.expression(yz))[1] "pexp(3.2,rate=1)">But that doesn't work. Any suggestions would be most welcome. I have a feeling that it's quite simple and that I'm having a forest vs. trees issue. Thanks, Erin -- Erin Hodgess Associate Professor Department of Computer and Mathematical Sciences University of Houston - Downtown mailto: erinm.hodgess at gmail.com
Good morning Erin, eval(parse(text = "pexp(3.2,rate=1)")) seems to work But the general rule applies: library(fortunes) fortune("parse()") Best, Michael On Tue, Nov 22, 2011 at 1:23 PM, Erin Hodgess <erinm.hodgess at gmail.com> wrote:> Dear R People: > > Hope you're having a nice day. > > Here is a character vector: > >> yz > [1] "pexp(3.2,rate=1)" >> str(yz) > ?chr "pexp(3.2,rate=1)" >> > And I would like to evaluate that vector. > > I tried: >> eval(as.expression(yz)) > [1] "pexp(3.2,rate=1)" >> > > But that doesn't work. > > Any suggestions would be most welcome. ?I have a feeling that it's > quite simple and that I'm having a forest vs. trees issue. > > Thanks, > Erin > > > -- > Erin Hodgess > Associate Professor > Department of Computer and Mathematical Sciences > University of Houston - Downtown > mailto: erinm.hodgess at gmail.com > > ______________________________________________ > 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 22.11.2011 19:23, Erin Hodgess wrote:> Dear R People: > > Hope you're having a nice day. > > Here is a character vector: > >> yz > [1] "pexp(3.2,rate=1)" >> str(yz) > chr "pexp(3.2,rate=1)" >> > And I would like to evaluate that vector. > > I tried: >> eval(as.expression(yz)) > [1] "pexp(3.2,rate=1)" >> > > But that doesn't work. > > Any suggestions would be most welcome. I have a feeling that it's > quite simple and that I'm having a forest vs. trees issue.Erin, you need to parse() the text before eval()uating it. In most cases, the original problem is why you got the text and not already something that is a language object. Best, Uwe> > Thanks, > Erin > >
On 23/11/11 07:31, R. Michael Weylandt wrote:> Good morning Erin, > > eval(parse(text = "pexp(3.2,rate=1)")) > > seems to work > > But the general rule applies: > > library(fortunes) > fortune("parse()")The fortune notwithstanding I find this *specific* use of parse() to be very, uh, useful! :-) cheers, Rolf Turner
On Nov 23, 2011, at 00:24 , Rolf Turner wrote:>> >> library(fortunes) >> fortune("parse()") > The fortune notwithstanding I find this *specific* use of parse() to be > very, uh, useful! :-)The fortune does say "usually", and there certainly are exceptions, for instance the use in Rcmdr where you edit and submit code from the log window. What to avoid is something like this: value <- 3.2; r <- 1 cmd <- paste("pexp(",value,",rate=",r,")") eval(parse(text=cmd)) where pexp(value,rate=r) would do. Or, if you do need the unevaluated expression: bquote(pexp(.(value),rate=.(r))) (I believe we still have a couple of cases in the standard R code where the code does something like rhs <- paste(vnames, sep="+") and then proceeds to parse it and put the result into the right hand side of a model formula. It works (mostly) but it hurts my eyes seeing computing on the language done via the textual representation.) -- Peter Dalgaard, Professor Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com