Sebastien Bihorel
2018-May-03 03:11 UTC
[R] Calling the curve function with a character object converted into an expression
Hi, Down a cascade of function calls, I want to use the curve function with an expression that is a variable. For various reason, this variable must be a character object and cannot be an expression as required by the curve function. How do I convert my variable into a expression that is accepted by curve? Thanks in advance for your help. ## The following attempts do not work myf <- '1+x^2' curve(myf, from = 0, to = 10) # obviously ! curve(parse(text=myf), from = 0, to = 10) # not sure why this does not work ## This works but does not feel elegant: eval(parse(text=sprintf('curve(%s, from = 0, to = 10)', myf)))
Bert Gunter
2018-May-03 05:35 UTC
[R] Calling the curve function with a character object converted into an expression
Sebastian: This is somewhat arcane, perhaps even a bug (correction on this welcomed). The problem is that the "expr" argument to curve() must be an actual expression, not a call to parse that evaluates to an expression. If you look at the code of curve() you'll see why (substitute() does not evaluate expr in the code). Another simple workaround other than sticking in the eval() call is this: myf <- function(x)NUL body(myf)<- parse(text = "{1+x^2}") ## note the additional "{ }" for safety, though not necessary here ## this idiom will continue to work for any text. curve(myf, from = 0, to = 10) ## myf is now the name of a function that executes the parsed text. *** I would appreciate any wiser R programmers correcting any misunderstanding or error in my explanation *** Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Wed, May 2, 2018 at 8:11 PM, Sebastien Bihorel <sebastien.bihorel at cognigencorp.com> wrote:> > Hi, > > Down a cascade of function calls, I want to use the curve function with an expression that is a variable. For various reason, this variable must be a character object and cannot be an expression as required by the curve function. How do I convert my variable into a expression that is accepted by curve? > > Thanks in advance for your help. > > ## The following attempts do not work > > myf <- '1+x^2' > curve(myf, from = 0, to = 10) # obviously ! > curve(parse(text=myf), from = 0, to = 10) # not sure why this does not work > > ## This works but does not feel elegant: > eval(parse(text=sprintf('curve(%s, from = 0, to = 10)', myf))) > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Bert Gunter
2018-May-03 06:28 UTC
[R] Calling the curve function with a character object converted into an expression
Typo: should be NULL not NUL of course An alternative approach closer to your original attempt is to use do.call() to explicitly evaluate the expr argument: w <- "1 + x^2" do.call(curve, list(expr = parse(text = w), ylab ="y")) Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Wed, May 2, 2018 at 10:35 PM, Bert Gunter <bgunter.4567 at gmail.com> wrote:> Sebastian: > > This is somewhat arcane, perhaps even a bug (correction on this > welcomed). The problem is that the "expr" argument to curve() must be > an actual expression, not a call to parse that evaluates to an > expression. If you look at the code of curve() you'll see why > (substitute() does not evaluate expr in the code). Another simple > workaround other than sticking in the eval() call is this: > > myf <- function(x)NUL > body(myf)<- parse(text = "{1+x^2}") ## note the additional "{ }" > for safety, though not necessary here > ## this idiom will continue to work for any text. > > curve(myf, from = 0, to = 10) ## myf is now the name of a function > that executes the parsed text. > > *** I would appreciate any wiser R programmers correcting any > misunderstanding or error in my explanation *** > > Cheers, > Bert > > Bert Gunter > > "The trouble with having an open mind is that people keep coming along > and sticking things into it." > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) > > > On Wed, May 2, 2018 at 8:11 PM, Sebastien Bihorel > <sebastien.bihorel at cognigencorp.com> wrote: >> >> Hi, >> >> Down a cascade of function calls, I want to use the curve function with an expression that is a variable. For various reason, this variable must be a character object and cannot be an expression as required by the curve function. How do I convert my variable into a expression that is accepted by curve? >> >> Thanks in advance for your help. >> >> ## The following attempts do not work >> >> myf <- '1+x^2' >> curve(myf, from = 0, to = 10) # obviously ! >> curve(parse(text=myf), from = 0, to = 10) # not sure why this does not work >> >> ## This works but does not feel elegant: >> eval(parse(text=sprintf('curve(%s, from = 0, to = 10)', myf))) >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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.
Reasonably Related Threads
- Calling the curve function with a character object converted into an expression
- Calling the curve function with a character object converted into an expression
- Problem with viewports, print.trellis and more/newpage
- Understanding padding in lattice
- Plotting a function with curve()