Skip to content

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

2 messages · bogdan romocea, Brian Ripley

#
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()
#
The example on the png help page, "Rplot%03d.png", _is_ what you want.
(More details are on ?postscript.)

formatC() is an S/R peculiarity: sprintf() is the cross-language way to do 
this sort of thing.
On Sat, 8 Oct 2005, bogdan romocea wrote: