[R] xyplot() groups scope issue

2007-08-30 Thread Mike Lawrence
Hi all,

Tinkering with a wrapper for xyplot that will help me plot a bunch of  
plots in a data analysis I'm doing and I ran into an odd error that  
I'm guessing is a scope issue. Here's a very simple version of the code:


###
#load lattice
library(lattice)

#create the wrapper function
do.xyplot = function( plot.formula, data.frame, plot.groups){

print(plot.groups) #show me what the wrapper function thinks  
'plot.groups' is

xyplot(
plot.formula
,data = data.frame
,groups = eval(plot.groups)
)
}

#create some data
mydata = as.data.frame(cbind( x = c(1:4), y = rep(1:2), z = rep(1:2,  
each = 2) ))

#try to plot the data (fails)
do.xyplot(
plot.formula=formula(x~y)
,data.frame = mydata
,plot.groups = expression(z)
)


#after error message try again, this time declaring plot.groups as a  
global variable (succeeds)
plot.groups = expression(z)
do.xyplot(
plot.formula=formula(x~y)
,data.frame = mydata
,plot.groups = expression(z)
)

#

I'm fine with declaring plot.groups before each call to do.xyplot,  
but I'm curious if there's a simpler solution.

Mike

--
Mike Lawrence
Graduate Student, Department of Psychology, Dalhousie University

Website: http://memetic.ca

Public calendar: http://icalx.com/public/informavore/Public

The road to wisdom? Well, it's plain and simple to express:
Err and err and err again, but less and less and less.
- Piet Hein

__
R-help@stat.math.ethz.ch 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.


Re: [R] xyplot() groups scope issue

2007-08-30 Thread deepayan . sarkar
On 8/30/07, Mike Lawrence [EMAIL PROTECTED] wrote:
 Hi all,

 Tinkering with a wrapper for xyplot that will help me plot a bunch of
 plots in a data analysis I'm doing and I ran into an odd error that
 I'm guessing is a scope issue. Here's a very simple version of the code:

It's indeed a scoping issue. For writing wrappers, the best solution
I've been able to come up with involves match.call() and
eval.parent(). See lattice:::dotplot.formula for a template.

If you really want to do it your way, try changing the environment of
the formula:

do.xyplot = function( plot.formula, data.frame, plot.groups){
print(plot.groups)
environment(plot.formula) - environment() # new
xyplot(
plot.formula
,data = data.frame
,groups = eval(plot.groups)
)
}

-Deepayan

__
R-help@stat.math.ethz.ch 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.