[R] evaluation of equations from Ryacas

William Dunlap wdunlap at tibco.com
Wed Jun 19 17:52:03 CEST 2013


> > e<-expression(list(R == 100 * (1 - 2 * y/10000)))
> > ee <- e[[1]][2]
> > ee
> (R == 100 * (1 - 2 * y/10000))()
> > eval(parse(text=(substring(paste(ee), 5))), list(y=5))
> [1] 99.9

The following avoids the fragile substring(paste(expression)) business:
  > eval(e[[1]][[2]][[3]], list(y=5))
  [1] 99.9
The [[1]] pulls the call to list out of the expression object.
The [[2]] pulls the first (and only) argument out of the call to list.
I assume that argument is a call to '==' with 2 arguments ('R' and
'100 * (1 -2 * y/10000)') so the [[3]] pulls out the second argument.
This solution may be fragile as well, but it doesn't depend on you
knowing the number of characters in the variable names.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Søren Højsgaard
> Sent: Wednesday, June 19, 2013 12:06 AM
> To: Erin Hodgess; R help
> Subject: Re: [R] evaluation of equations from Ryacas
> 
> Dear Erin,
> 
> Not exactly elegant, but
> 
> > e<-expression(list(R == 100 * (1 - 2 * y/10000)))
> > ee <- e[[1]][2]
> > ee
> (R == 100 * (1 - 2 * y/10000))()
> > eval(parse(text=(substring(paste(ee), 5))), list(y=5))
> [1] 99.9
> 
> Regards
> Søren
> 
> 
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Erin Hodgess
> Sent: 19. juni 2013 07:03
> To: R help
> Subject: [R] evaluation of equations from Ryacas
> 
> Hello again.
> 
> Now I have the following:
> > xx
> [1] "Solve(1 - R/100==(2*y)/10000,R)"
> > yacas(xx)
> expression(list(R == 100 * (1 - 2 * y/10000)))
> >
> 
> I would like to put in a value for y and obtain R.
> I've tried more stuff with eval and Eval, but no luck yet.
> 
> Any suggestions would be much appreciated.
> 
> Thanks,
> Erin
> 
> 
> --
> Erin Hodgess
> Associate Professor
> Department of Computer and Mathematical Sciences University of Houston - Downtown
> mailto: erinm.hodgess at gmail.com
> 
> 	[[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.
> 
> ______________________________________________
> 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.



More information about the R-help mailing list