[R] plotting an expression

apjaworski@mmm.com apjaworski at mmm.com
Tue May 1 00:20:56 CEST 2001


I am sure it is just me not understanding how R works, but could somebody
explain why

     curve(cos(x))

works and

     curve(expression(cos(x))

does not?

I have done some investigating and here is what I found.  If I comment out
the line of curve indicated below, both calls work fine.

function (expr, from, to, n = 101, add = FALSE, type = "l", ylab = NULL,
    log = NULL, xlim = NULL, ...)
{
    sexpr <- substitute(expr)
    if (is.name(sexpr)) {
        fcall <- paste(sexpr, "(x)")
        expr <- parse(text = fcall)
        if (is.null(ylab))
            ylab <- fcall
    }
    else {
        if (!(is.call(sexpr) && match("x", all.vars(sexpr), nomatch = 0)))
            stop("'expr' must be a function or an expression containing
'x'")
 #     expr <- sexpr
        if (is.null(ylab))
            ylab <- deparse(sexpr)
    }
    lims <- if (is.null(xlim))
    < ... >

This is confusing, since

     substitiue(expression(cos(x)))

returns just

     expression(cos(x)).

Must be some side effects I do not understand.  What is even more confusing
is that the above code (with one line commented out) does not work when
called as

     curve(parse(text="cos(x)"))

although

     parse(text="cos(x)")

returns

     expression(cos(x)).


What I am really trying to do is plot a rather compilcated expression that
has been built from pieces by pasting strings together.  SInce the parse
function returns an expression I thought I could just feed the result of
parse to plot ( or curve).

Any help or explanation will be appreciated,

Andy

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list