Laura Quinn
2004-Nov-14 13:44 UTC
[R] Exporting to file: passing source name to file name in loop
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
Prof Brian Ripley
2004-Nov-14 14:00 UTC
[R] Exporting to file: passing source name to file name in loop
What does `it doesn't work' actually mean? Please read the posting guide and follow its advice. Also, please show us readable and properly indented code, using spaces consistently. (There is a chapter in `Writing R Extensions' showing you how to do this.) I just cannot parse your code, and suspect you cannot either. On Sun, 14 Nov 2004, Laura Quinn wrote:> 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 > > ______________________________________________ > R-help at 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 > >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Laura Quinn
2004-Nov-14 15:16 UTC
[R] Exporting to file: passing source name to file name in loop
Apologies - Sunday afternoon coding, not my forte.
I am trying to pass the name of my input variable "x" (my.data) into
the
name of my output file, and am wanting to combine this with a "count"
value.
I hope that this code is a little more readable (though I seem to be
having problems pasting the formatting into my email composition):
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)){
plot.new()
png(file=(paste(as.character(x),".",i,".png",sep="")),width=240,height=240)
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()
}
dev.off()
}
}
I get a "syntax error" relating to the as.character(x) part of the
file
name - if I remove this, the code works fine for the rest of the file
extension. I have tried deparse(x) but this returns a file extension of
the following nature:
structure(list(f1 = c(5.56358661715647, 6.10364037003176,
6.24040147126807, .10.png
Thanks,
Laura
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