[R] Calling the curve function with a character object converted into an expression

Sebastien Bihorel @eb@@tien@bihorel @ending from cognigencorp@com
Thu May 3 05:11:56 CEST 2018


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)))

More information about the R-help mailing list