Re: [S] passing argument to strip.default()

Prof Brian Ripley (ripley@stats.ox.ac.uk)
Fri, 6 Mar 1998 17:40:32 GMT


Jan Schelling <schelling@ivuk.mavt.ethz.ch> wrote

> does anybody know how to pass an argument to the strip.default() function
> inside a call to a trellis plotting routine?

Why do people ask if anyone knows, rather than ask how to do it?

> In detail: I'm trying to customize the text that is printed on the strips
> of a multi-panel xy-plot.
>
> My call looks something like this:
>
> plot.test <-
> function(data, x, y, z, n, ...) {
> z.levels <- rev(sort(unique(data[,z])))
> z.factor <- factor(data[,z], levels=z.levels)
> xyplot(data[,y]~data[,x]|z.factor,
> strip=function(...) strip.default(..., style=n),
> ...)
> }
>
> For example, I'd like to pass the parameter n to the style argument of
> strip.default().
>
> With
> d <- data.frame(x=rnorm(100), y=rnorm(100), z=rep(1:2,rep(50,2)))
> plot.test(data=d, x="x", y="y", z="z", n=5)
>
> I get the following error message:
>
> Error in strip(k, unlist(index[n, ]), gnames, factor = if(v) NULL else
> g..: Object "n" not found
>
> If I use
> strip=function(...) strip.default(..., style=5),
> directly everything is fine.
>
> What am I missing here?
>
The scope rules of S! Try

plot.test <-
function(data, x, y, z, n, ...) {
assign("style.n", n, frame = 1)
z.levels <- rev(sort(unique(data[,z])))
z.factor <- factor(data[,z], levels=z.levels)
print(xyplot(data[,y]~data[,x]|z.factor,
strip=function(...) strip.default(..., style=style.n),
...))
invisible()
}

You need to put n somewhere where it is visible to the deeply embedded
call to plot. I've given it a less easily confused name.

I've also corrected another misunderstanding: you need to
print a trellis plot to get the output. Your function works by returning a
pre-digested object that is rendered by auto-printing, but this is
not transparent and error-prone. Better to print explicitly.

-- 
Brian D. Ripley,                  ripley@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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news