Dear List members, I really, like the feature that one can call R functions with mathematical expressions, e.g. curve(x^2, 0, 1) I wonder, how I can construct in a simple way a function like mycurve = function (expr) {...} such that that a call mycurve(x^2) has the same effect as the call curve(x^2, -100,100) Below is some code that works, but it seems much to complicated: it first substitutes and deparses the expression, creates a string with the new function call and then parses and evaluates the string again. Does anybody know a simpler, more elegant solution? mycurve = function(expr) { # The following attempt does not work # curve(substitute(expr),-100,100) # Transform original expression to a string org.expr = deparse(substitute(expr)) # Construct a string, parse it and evaluates it eval(parse(text=paste("curve(",org.expr,",-100,100)",sep=""))) } mycurve(x^2) Best regards, Sebastian [[alternative HTML version deleted]]
Hi Sebastian, how about this: mycurve <- function (expr) { do.call(curve,list(substitute(expr),-100,100)) } mycurve(x^2) mycurve(sin(x/20)) mycurve(x+x^2-x^3) cheers Am 21.02.2012 06:28, schrieb Sebastian Kranz:> Dear List members, > > I really, like the feature that one can call R functions with > mathematical expressions, e.g. > > curve(x^2, 0, 1) > > > I wonder, how I can construct in a simple way a function like > > mycurve = function (expr) {...} > > such that that a call > > mycurve(x^2) > > has the same effect as the call > > curve(x^2, -100,100) > > Below is some code that works, but it seems much to complicated: it > first substitutes and deparses the expression, creates a string with the > new function call and then parses and evaluates the string again. Does > anybody know a simpler, more elegant solution? > > mycurve = function(expr) { > # The following attempt does not work > # curve(substitute(expr),-100,100) > > # Transform original expression to a string > org.expr = deparse(substitute(expr)) > # Construct a string, parse it and evaluates it > eval(parse(text=paste("curve(",org.expr,",-100,100)",sep=""))) > } > mycurve(x^2) > > Best regards, > Sebastian > > [[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.-- Eik Vettorazzi Institut f?r Medizinische Biometrie und Epidemiologie Universit?tsklinikum Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/7410-58243 F ++49/40/7410-57790 -- Pflichtangaben gem?? Gesetz ?ber elektronische Handelsregister und Genossenschaftsregister sowie das Unternehmensregister (EHUG): Universit?tsklinikum Hamburg-Eppendorf; K?rperschaft des ?ffentlichen Rechts; Gerichtsstand: Hamburg Vorstandsmitglieder: Prof. Dr. Guido Sauter (Vertreter des Vorsitzenden), Dr. Alexander Kirstein, Joachim Pr?l?, Prof. Dr. Dr. Uwe Koch-Gromus
Thanks, that is exactly what I have been looking for! On Tue, 21 Feb 2012 11:58:01 +0100 Eik Vettorazzi <E.Vettorazzi at uke.de> wrote:> Hi Sebastian, > how about this: > > mycurve <- function (expr) { > do.call(curve,list(substitute(expr),-100,100)) > } > > mycurve(x^2) > mycurve(sin(x/20)) > mycurve(x+x^2-x^3) > > cheers > > Am 21.02.2012 06:28, schrieb Sebastian Kranz: >> Dear List members, >> >> I really, like the feature that one can call R functions >>with >> mathematical expressions, e.g. >> >> curve(x^2, 0, 1) >> >> >> I wonder, how I can construct in a simple way a function >>like >> >> mycurve = function (expr) {...} >> >> such that that a call >> >> mycurve(x^2) >> >> has the same effect as the call >> >> curve(x^2, -100,100) >> >> Below is some code that works, but it seems much to >>complicated: it >> first substitutes and deparses the expression, creates a >>string with the >> new function call and then parses and evaluates the >>string again. Does >> anybody know a simpler, more elegant solution? >> >> mycurve = function(expr) { >> # The following attempt does not work >> # curve(substitute(expr),-100,100) >> >> # Transform original expression to a string >> org.expr = deparse(substitute(expr)) >> # Construct a string, parse it and evaluates it >> eval(parse(text=paste("curve(",org.expr,",-100,100)",sep=""))) >> } >> mycurve(x^2) >> >> Best regards, >> Sebastian >> >> [[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. > > > -- > Eik Vettorazzi > Institut f?r Medizinische Biometrie und Epidemiologie > Universit?tsklinikum Hamburg-Eppendorf > > Martinistr. 52 > 20246 Hamburg > > T ++49/40/7410-58243 >F ++49/40/7410-57790 > > -- > Pflichtangaben gem?? Gesetz ?ber elektronische >Handelsregister und Genossenschaftsregister sowie das >Unternehmensregister (EHUG): > > Universit?tsklinikum Hamburg-Eppendorf; K?rperschaft des >?ffentlichen Rechts; Gerichtsstand: Hamburg > > Vorstandsmitglieder: Prof. Dr. Guido Sauter (Vertreter >des Vorsitzenden), Dr. Alexander Kirstein, Joachim Pr?l?, >Prof. Dr. Dr. Uwe Koch-Gromus >