Dgnn
2012-Mar-13 19:55 UTC
[R] Writing a .pdf file within a function - what do I need to return()?
I am trying to write a function that generates one PDf containing plots from several .csv files within a directory. When I manually execute the code it seems to work, but not when it is a function. I think I need to return() something, but haven't had much luck figuring out what/how. plot.isi<-function(csv.path="~/project/csv by cell") { csv.files<-grep('.csv', list.files(path = csv.path, full.names=T), value=T) pdf(file='plots/isi plots.pdf', width=10, height=8) #par(mfrow=c(2,1)) #ideally 2 plots per page, but will work on details after fx. works for (i in 1:length(csv.files)){ raw.df<-read.csv(csv.files[i]) names(raw.df)<-c('t','isi','logic','cond') xyplot(isi ~ t, raw.df, ylim=c(0,1500), ylab='isi', xlab='time', main=basename(csv.files[i])) } dev.off() } Thank you all for the help, Jason Deignan -- View this message in context: http://r.789695.n4.nabble.com/Writing-a-pdf-file-within-a-function-what-do-I-need-to-return-tp4470165p4470165.html Sent from the R help mailing list archive at Nabble.com.
R. Michael Weylandt
2012-Mar-14 03:27 UTC
[R] Writing a .pdf file within a function - what do I need to return()?
See R FAQ 7.22 -- in short, you need to print() your plot to the graphics device -- just wrap xyplot() in print() and it should work. Michael On Tue, Mar 13, 2012 at 3:55 PM, Dgnn <sharkbrainpdx at gmail.com> wrote:> I am trying to write a function that generates one PDf containing plots from > several .csv files within a directory. ?When I manually execute the code it > seems to work, but not when it is a function. I think I need to return() > something, but haven't had much luck figuring out what/how. > > plot.isi<-function(csv.path="~/project/csv by cell") { > ? ? ? ?csv.files<-grep('.csv', list.files(path = csv.path, full.names=T), value=T) > ? ? ? ?pdf(file='plots/isi plots.pdf', width=10, height=8) > ? ? ? ?#par(mfrow=c(2,1)) #ideally 2 plots per page, but will work on details > after fx. works > ? ? ? ?for (i in 1:length(csv.files)){ > ? ? ? ? ? ? ? ?raw.df<-read.csv(csv.files[i]) > ? ? ? ? ? ? ? ?names(raw.df)<-c('t','isi','logic','cond') > ? ? ? ? ? ? ? ?xyplot(isi ~ t, raw.df, ylim=c(0,1500), ylab='isi', xlab='time', > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?main=basename(csv.files[i])) > ? ? ? ?} > ? ? ? ?dev.off() > } > > Thank you all for the help, > > Jason Deignan > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Writing-a-pdf-file-within-a-function-what-do-I-need-to-return-tp4470165p4470165.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org 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.