[R] Using substitute in nested function calls
Eik Vettorazzi
E.Vettorazzi at uke.de
Tue Feb 21 11:58:01 CET 2012
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
More information about the R-help
mailing list