Possible Duplicate:
Generate multiple graphics from within an R function
I'm trying to store all the plot from all the variables in my data with histogram from the package lattice. When I run directly, it works but if I use a function don't create the figures. My function is:
plotHistByGroups<-function(data, res)
{
jpeg("plots/histBy%03d.jpg", quality=100)
finalres<-as.factor(res)
names<-colnames(data)
library(lattice)
for( i in 1: length(data))
{
title<-paste(names[i], "~")
title<-paste(title, length(levels(finalres)))
title<-paste(title, "clusters")
histogram(~data[,i] | finalres, data, xlab=names[i], main=title)
}
dev.off()
}
When I call the function, I only get one empty figure. But, I obtain all the figures If I directly run:
jpeg("plots/histBy%03d.jpg", quality=100)
histogram(~data[,1] | finalres, data, xlab=names[1], main=title)
histogram(~data[,2] | finalres, data, xlab=names[2], main=title)
histogram(~data[,3] | finalres, data, xlab=names[3], main=title)
...
dev.off()
Bea