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

Charilaos Skiadas cskiadas at gmail.com
Sun Mar 9 20:35:18 CET 2008


On Mar 9, 2008, at 1:29 PM, Prof Brian Ripley wrote:

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

Thanks, I have indeed not gotten into the habit of using R's  
debugging tools, it would have helped me considerably in this case,  
as would reading ?plot.formula.
It is interesting, that in the actual case where I am using this I am  
passing a number of other graphics parameters, and it so happened all  
the others were constants (well OK, maybe "interesting" is not the  
correct word).

I am guessing this behaviour of plot.formula is so as to allow the  
use of variables from the data frame in the specification of graphics  
parameters like col, lty etc?

Your suggestion does indeed work very nicely in my case. I could  
probably do away from the formula interface altogether, but it is a  
convenient way of specifying the axis labels in my case.

Haris Skiadas
Department of Mathematics and Computer Science
Hanover College


> 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