[R] R-level expansion of Rplot%03d.png

baptiste auguie baptiste.auguie at googlemail.com
Sat Aug 21 19:26:16 CEST 2010


Dear Prof. Ripley,

Thank you for the authoritative information. I will implement my own
file numbering scheme, but I'm surprised it was never asked for in all
those years. It seems like a useful feature, to know what your output
filename will be.

Thanks again,

baptiste

On 21 August 2010 18:27, Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:
> Just so this thread is complete on the record:
>
> Filenames such as "Rplot%03d.png" are used in C code as format strings when
> opening a file.  Nothing in R subsequently knows the file name -- the file
> is accessed through the FILE* pointer.
>
> Despite what baptiste 'assumes', it is clearly documented that the string is
> used to format the *page number*: there is no regex search. E.g. (?png,
> Unix)
>
> filename: the name of the output file.  The page number is substituted
>          if a C integer format is included in the character string, as
>          in the default.
>
> There is no API to find the current page number, nor AFAIR in all the years
> of the R graphics device API has anyone asked for one.
>
> If I wanted to do anything like this I would expect to generate filenames
> myself and open a new png() device for each plot.  But the alternative is to
> count the number of pages your code generates (which may need detailed
> knowledge of what the plot commands do).
>
>
> On Sat, 21 Aug 2010, kees duineveld wrote:
>
>> You are correct. (I really should start using these reading glasses).
>>
>> My apologies
>>
>> On Sat, 21 Aug 2010 14:20:17 +0200, baptiste auguie
>> <baptiste.auguie at googlemail.com> wrote:
>>
>>> I dunno, it doesn't seem to do it for me,
>>>
>>> name = "Rplot%03d.png"
>>> real.name = path.expand(name)
>>> real.name
>>> #[1] "Rplot%03d.png"
>>> list.files(patt=".png")
>>> #[1] "Rplot001.png"
>>>
>>> sessionInfo()
>>> R version 2.11.1 (2010-05-31)
>>> i386-apple-darwin9.8.0
>>>
>>> locale:
>>> [1] en_GB.UTF-8/en_GB.UTF-8/C/C/en_GB.UTF-8/en_GB.UTF-8
>>>
>>> attached base packages:
>>> [1] stats     graphics  grDevices utils     datasets  methods   base
>>>
>>>
>>>
>>> On 21 August 2010 14:15, kees duineveld <kees.duineveld at gmail.com> wrote:
>>>>
>>>> Now I understand. You need the name which png() does not return.
>>>> So I think you need to do (untested, I am struggling with the cat()):
>>>>
>>>>
>>>> makePlot = function(p, name="Rplot%03d", width=300)
>>>> {
>>>> real.name.png = path.expand(paste(name,'.png'sep='') # function needed
>>>> here
>>>> real.name.pdf = path.expand(paste(name,'.pdf'sep='') # function needed
>>>> here
>>>> png(real.name.png)
>>>> print(p)
>>>> dev.off()
>>>> pdf(real.name.pdf)
>>>> print(p)
>>>> dev.off()
>>>>
>>>> cat(noquote(paste('image:',real.name.png,',width=',width,',link=real.name.pdf)
>>>> }
>>>>
>>>> On Sat, 21 Aug 2010 14:02:04 +0200, baptiste auguie
>>>> <baptiste.auguie at googlemail.com> wrote:
>>>>
>>>>> My function needs to do two things with the filename:
>>>>>
>>>>> First, create the plot file. For this, Rplot%03d is OK because it is
>>>>> correctly interpreted by the graphics device.
>>>>>
>>>>> Second, generate a text string referring to this filename. This is
>>>>> where I need to convert Rplot%03d to, say, Rplot001. I am assuming
>>>>> that it is implemented internally by looking at the files in the
>>>>> current directory with some regular expression search, and
>>>>> incrementing the end number as needed. I wonder if there's a high
>>>>> level function to perform this task.
>>>>>
>>>>> Best,
>>>>>
>>>>> baptiste
>>>>>
>>>>> On 21 August 2010 13:35, kees duineveld <kees.duineveld at gmail.com>
>>>>> wrote:
>>>>>>
>>>>>> Not sure what you want. Plot does that automatically. It seems to use
>>>>>> path.expand() to make the %03d expansion. Not that path.expand() is
>>>>>> documented to do this, but it seem to work.
>>>>>>
>>>>>> Kees
>>>>>>
>>>>>> On Sat, 21 Aug 2010 13:04:54 +0200, baptiste auguie
>>>>>> <baptiste.auguie at googlemail.com> wrote:
>>>>>>
>>>>>>> Dear list,
>>>>>>>
>>>>>>> I'm using the brew package to generate a report containing various
>>>>>>> plots. I wrote a function that creates a plot in png and pdf formats,
>>>>>>> and outputs a suitable text string to insert the file in the final
>>>>>>> document using the asciidoc syntax,
>>>>>>>
>>>>>>> <%
>>>>>>> tmp <- 1
>>>>>>> makePlot = function(p, name=paste("tmp",tmp,sep=""), width=300)
>>>>>>> {
>>>>>>> png(paste(name,".png",sep=""))
>>>>>>> print(p)
>>>>>>> dev.off()
>>>>>>> pdf(paste(name,".pdf",sep=""))
>>>>>>> print(p)
>>>>>>> dev.off()
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> cat(noquote(paste('image:',name,'.png["',name,'",width=',width,',link="',name,'.pdf"]',sep="")))
>>>>>>> tmp <<- tmp + 1
>>>>>>> }
>>>>>>> %>
>>>>>>>
>>>>>>> The resulting html file contains a thumbnail of the png file, with a
>>>>>>> link to the pdf file.
>>>>>>>
>>>>>>> I'm not happy with my default filename for the graphics. Is there a
>>>>>>> way to expand the default filename of R graphic devices? I would like
>>>>>>> to call it like this,
>>>>>>>
>>>>>>>
>>>>>>> makePlot = function(p, name="Rplot%03d", width=300)
>>>>>>> {
>>>>>>>
>>>>>>> real.name = expandName(name) # function needed here
>>>>>>> png(paste(name,".png",sep=""))
>>>>>>> print(p)
>>>>>>> dev.off()
>>>>>>> pdf(paste(name,".pdf",sep=""))
>>>>>>> print(p)
>>>>>>> dev.off()
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> cat(noquote(paste('image:',real.name,'.png["',real.name,'",width=',width,',link="',real.name,'.pdf"]',sep="")))
>>>>>>> }
>>>>>>>
>>>>>>>
>>>>>>> Sincerely,
>>>>>>>
>>>>>>> baptiste
>>>>>>>
>>>>>>> ______________________________________________
>>>>>>> R-help at r-project.org mailing list
>>>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>>>> PLEASE do read the posting guide
>>>>>>> http://www.R-project.org/posting-guide.html
>>>>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>>>
>>>>>>
>>>>>> --
>>>>>>
>>>>>> ______________________________________________
>>>>>> R-help at r-project.org mailing list
>>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>>> PLEASE do read the posting guide
>>>>>> http://www.R-project.org/posting-guide.html
>>>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>>>
>>>>
>>>>
>>>> --
>>>> Using Opera's revolutionary email client: http://www.opera.com/mail/
>>>>
>>
>>
>> --
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> --
> 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
>



-- 
____________________

Dr. Baptiste Auguié

Departamento de Química Física,
Universidade de Vigo,
Campus Universitario, 36310, Vigo, Spain

tel: +34 9868 18617
http://webs.uvigo.es/coloides



More information about the R-help mailing list