Hi: There, I've got a question about how to read in expressions as function parameters and it really bothered me. I'm going to use curve() function to plot curves, and I'd like to write a menu function to let use input math expressions. say, if I'd like curve(3*x*x-4/x, 10, 100), I use scan() to read in the math expression form<- scan(file = "", what = character(0), n=1,strip.white TRUE,quiet=TRUE ) then get form as a string. "3*x*x-4/x" Use parse() to convert it into expression form2<-parse(text=form) then form2=expression(3*x*x-4/x). However, curve(form2,10,100) doesn't work at all. Is there anyway to get this done? Thanks a lot! Alex [[alternative HTML version deleted]]
Henrique Dallazuanna
2007-Dec-10 15:34 UTC
[R] How to read in expressions as function parameters?
Try this:
form<- scan(file = "", what = character(0), n=1,strip.white
TRUE,quiet=TRUE )
form2<-parse(text=form)
foo <- function(x){}
body(foo) <- form2
curve(foo,10,100)
On 09/12/2007, Alex Wang <alecwang80 at gmail.com>
wrote:> Hi:
>
> There, I've got a question about how to read in expressions as
function
> parameters and it really bothered me.
>
> I'm going to use curve() function to plot curves, and I'd like to
write a
> menu function to let use input math expressions.
>
> say, if I'd like curve(3*x*x-4/x, 10, 100), I use scan() to read in
the
> math expression
>
> form<- scan(file = "", what = character(0), n=1,strip.white
> TRUE,quiet=TRUE )
> then get form as a string. "3*x*x-4/x"
> Use parse() to convert it into expression
> form2<-parse(text=form)
> then form2=expression(3*x*x-4/x).
>
> However, curve(form2,10,100) doesn't work at all.
>
> Is there anyway to get this done?
>
> Thanks a lot!
>
> Alex
>
> [[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.
>
--
Henrique Dallazuanna
Curitiba-Paran?-Brasil
25? 25' 40" S 49? 16' 22" O