[R] xyplot() groups scope issue

Mike Lawrence Mike.Lawrence at DAL.CA
Thu Aug 30 18:17:26 CEST 2007


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



More information about the R-help mailing list