[R] add leading 0s to %d from png() {was Automatic creation of file names}

bogdan romocea br44114 at gmail.com
Sat Oct 8 17:26:26 CEST 2005


Dear useRs,

Is there a way to 'properly' format %d when plotting more than one
page on png()? 'Properly' means to me with leading 0s, so that the
PNGs become easy to navigate in a file/image browser. Lacking a better
solution I ended up using the code below, but would much prefer
something like
   png("test_%d.png",bg="white",width=1000,height=700)
where %d could be formatted like
   formatC(%d,digits=0,wid=3,flag="0",mode="integer")

Thank you,
b.

#---works, but is rather complicated---
pngno <- 0 ; i <- 1
for (w in 1:53) {
  if (i %in% c(4*0:100+1)) {
    pngno <- pngno + 1
    png(paste("test_",formatC(pngno,digits=0,wid=4,flag="0",mode="integer"),
      ".png",sep=""),bg="white",width=1000,height=750)
    par(mfrow=c(2,2),mai=c(4,5,3,2)/10,omi=c(0.2,0,0,0),
      cex.axis=1,cex.main=1.2)
    }
  plot(1:10,main=w)
  if (i %in% c(4*1:100)) dev.off()
  i <- i+1
  }
dev.off()


>From: Mike Prager <Mike.Prager <at> noaa.gov>
>Subject: Re: [R] Automatic creation of file names
>Newsgroups: gmane.comp.lang.r.general
>Date: 2005-09-22 14:51:54 GMT (2 weeks, 1 day, 23 hours and 55 minutes ago)
>
>Walter --
>
>P.S.  The advantage of using formatC over pasting the digits (1:1000)
>directly is that when one uses leading zeroes, as in the formatC example
>shown, the resulting filenames will sort into proper order.
>
>...MHP
>
>You can use paste() with something like
>
> formatC(number,digits=0,wid=3,flag="0")
>
>(where number is your loop index) to generate the filenames.
>
>on 9/22/2005 10:21 AM Leite,Walter said the following:
>
>>I have a question about how to save to the hard drive the one thousand
>>datasets I generated in a simulation. ://www.R-project.org/posting-guide.html




More information about the R-help mailing list