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

Prof Brian Ripley ripley at stats.ox.ac.uk
Sat Oct 8 17:41:39 CEST 2005


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:

> 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
>
> ______________________________________________
> 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




More information about the R-help mailing list