[R] Calling plot with a formula, from within a function, using ..., and xlim

Prof Brian Ripley ripley at stats.ox.ac.uk
Sun Mar 9 18:29:16 CET 2008


You should make a habit of using of R's debugging tools, e.g. traceback() 
which gives

> traceback()
6: eval(expr, envir, enclos)
5: FUN(X[[1L]], ...)
4: lapply(dots, eval, data, parent.frame())
3: plot.formula(k ~ j, data = obj, ...)
2: plot(k ~ j, data = obj, ...)
1: plotw(df, xlim = c(0, 4))

so it is not related to argument matching.  If you look closer (e.g. with 
options(error=recover)) you will see what 'dots' is in the two cases.

The help page for plot.formula() explains its scope rules.  Given those, 
it is not realistic to expect to be able to call it from within a 
function with ... arguments.

The difference in your two examples of plotw is that xaxs="i" has a 
constant value and xlim=c(0,4) is an expression.  And

> xs <- "i"
> plotw(df, xaxs=xs)

fails in the same way.

I reckon

> plotw <- function(obj,...)  do.call("plot", list(k~j, data=obj,...))

is the simplest way to do what you seem to be looking for, but it is 
probably just best to avoid functions like plot.formula with non-standard 
semantics when programming.


On Sun, 9 Mar 2008, Charilaos Skiadas wrote:

> I ran into a weird, to me at least, problem, and hoping someone can
> shed some light into it. In a nutshell, there seems to be some
> problem when one calls plot with a formula, from within another
> function, using ... to pass arguments, and one of those arguments
> being xlim (and only xlim shows this problem). Here is an example:
>
> > plotw <- function(obj,...) {
> +   plot(k~j, data=obj,...)
> + }
> > plotw2 <- function(obj,...) {
> +   plot(obj$j,obj$k, ...)
> + }
> > df <- data.frame(j=1:3,k=1:3)
> > plotw(df, las=1, xaxs="i")
> > plotw(df, xlim=c(0,4))
> Error in eval(expr, envir, enclos) :
>   ..1 used in an incorrect context, no ... to look in
> > plotw2(df, las=1, xaxs="i")
> > plotw2(df, xlim=c(0,4))
>
>
> I was guessing this might be related to some sort of argument
> matching, but I don't see why xaxs doesn't cause a problem in that
> case. So I am somewhat puzzled.
>
> Thanks in advance.
> Haris Skiadas
> Department of Mathematics and Computer Science
> Hanover College

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list