[R] Exporting to file: passing source name to file name in loop

Laura Quinn laura at env.leeds.ac.uk
Sun Nov 14 14:44:53 CET 2004


Hi,

I'm having a mental block as to how I can automatically assign filenames
to the output of the following code. I am wishing to create a separate
.png file for every image created, each of them having a sequential
filename ie "sourcefile_index.png" so that I can create a movie from
them.

Please could someone tell me where I am going wrong?

the following code works fine and outputs to screen:

fun_pca_vector_movie_plot<-function(x,y,z,t){
zz=seq(1,(nrow(x)-t),by=t);jj=seq(t+1,(nrow(x)),by=t)
for(i in seq(along=zz)){
         pca<-prcomp(x[zz[i]:jj[i],], retx=T, center=T,scale=T)
        {
     par(mfrow=c(1,z))
      for(i in 1:z){
       image(east,north,t(map.matrix),col=my.colors,axes=T,
           xlab="",ylab="")
       text(y[,3],y[,2],labels=as.character(y[,1]))
       title(paste("Component",i,"Step:"))
        arrows(y[,3],y[,2],(y[,3]+50*(pca$rotation[i,1:(ncol(x)/2)])),
        (y[,2]+50*(pca$rotation[i,((ncol(x)/2)+1):(ncol(x))])),
        angle=30,length=0.05,code=2)
      }
     box()
    }
   }}

but when I try to save to file as follows it doesn't work:

fun_pca_vector_movie_plot<-function(x,y,z,t){
zz=seq(1,(nrow(x)-t),by=t);jj=seq(t+1,(nrow(x)),by=t)
for(i in seq(along=zz)){
      pca<-prcomp(x[zz[i]:jj[i],], retx=T, center=T,scale=T)
    {
     par(mfrow=c(1,z))
       for(i in 1:z){
       plot.new()
       png(file=(paste(x".",i,"_",zz,".png",sep="")),width=240,height=240)
       image(east,north,t(map.matrix),col=my.colors,axes=T,
           xlab="",ylab="")
       text(y[,3],y[,2],labels=as.character(y[,1]))
       title(paste("Component",i,"Step:"))
        arrows(y[,3],y[,2],(y[,3]+50*(pca$rotation[i,1:(ncol(x)/2)])),
        (y[,2]+50*(pca$rotation[i,((ncol(x)/2)+1):(ncol(x))])),
        angle=30,length=0.05,code=2)
       dev.off()
      }
     box()
    }
   }}

many thanks in advance!
Laura Quinn
Institute of Atmospheric Science
School of Earth and Environment
University of Leeds
Leeds
LS2 9JT

tel: +44 113 343 1596
fax: +44 113 343 6716
mail: laura at env.leeds.ac.uk




More information about the R-help mailing list