[R] Setting default graphics device & options

Henrik Bengtsson hb at biostat.ucsf.edu
Sun May 26 19:25:27 CEST 2013


Sorry, forgot to add: "Hi, [a somewhat different approach but] see the
R.devices package...". /Henrik

On Sun, May 26, 2013 at 10:14 AM, Henrik Bengtsson <hb at biostat.ucsf.edu> wrote:
> Hi,
>
> see the R.devices package
> [http://cran.r-project.org/web/packages/R.devices/].   FYI, there is a
> vignette [R.devices-overview.pdf], but for some reason it's hard find.
>  However it is there: help.start() -> R.devices -> 'User guides,
> package vignettes and other documentation.' -> R.devices-overview.pdf.
>
> First, don't forget to call library("R.devices").
>
> CREATE IMAGE FILE ATOMICALLY:
> To create a PNG image file 'GaussianDensity.png' in subdirectory
> figures/ (of the current working directory), do:
>
> toPNG("GaussianDensity", aspectRatio=0.6, scale=2, {
>   curve(dnorm, from=-5, to=+5)
> })
>
> This will, while still using png()/dev.off() internally, (1)
> automatically add filename extension, (2) set the height of the image
> as 0.6 times the default width, (3) rescale height and width to be 2
> times the default, and (3) make sure the PNG device is closed
> afterward (no more forgetting about dev.off()).
>
> It's also make sure not to leave incomplete image files behind in case
> there's an error in your plot code.  There's an option to change that
> behavior too, e.g. so it instead renames the incomplete file for easy
> identification.
>
>
> DEFAULT OUTPUT DIRECTORY:
> You can set the default output directory as:
>
> options("devEval/args/path"="/srv/samba/share/")
>
> Then, whenever you call toPNG(), it will instead save the file to
> /srv/samba/share/.  If the directory is missing, it will be created
> automatically.
>
>
> DEFAULT FILENAME:
> Currently it is not possible to set a default filename pattern.
> However, you can do something like:
>
> imgname <- function() {
>   # NOTE: No filename extension
>   sprintf("Rplot-%s", format(Sys.time(), "%Y%m%d-%H%M%S"))
> }
>
> and then use:
>
> toPNG(imgname(), aspectRatio=0.6, scale=2, {
>   curve(dnorm, from=-5, to=+5)
> })
>
> (I'll think about adding support for a default image name format).
>
>
> DEFAULT DEVICE OPTIONS:
> To change the default image dimensions, do:
>
>> devOptions("png", width=1280, height=800);
>
> Importantly, in order for these devOptions() to apply, you must use
> toPNG() [or devEval("png")]; they won't apply if you call png()
> explicitly.  To check the default options, do:
>
>> str(devOptions("png"))
> List of 11
>  $ filename      : chr "Rplot%03d.png"
>  $ units         : chr "px"
>  $ pointsize     : num 12
>  $ bg            : chr "white"
>  $ res           : logi NA
>  $ family        : chr "sans"
>  $ restoreConsole: logi TRUE
>  $ type          : language c("windows", "cairo", "cairo-png")
>  $ antialias     : language c("default", "none", "cleartype", "grey", "subpixel"
> )
>  $ width         : num 1280
>  $ height        : num 800
>
> The default "defaults" are inferred from the default in R, so if you
> don't change anything you'll get the same output as calling
> png()/dev.off().
>
> BTW, unless all of your images should have aspect ratio
> 800/1280=0.625, I'd recommend to use square defaults (just as the
> png() device do), e.g. devOptions("png", width=1280, height=1280), and
> then specify aspectRatio=0.625 in your toPNG() calls.
>
>
> In addition to toPNG(), there are also toBMP(), toEPS(), toPDF(),
> toSVG() and toTIFF(), with their own devOptions() settings.
>
>
> Hope this is useful
>
> Henrik
>
> PS. From your example where all images have the same filename format
> with timestamps, it almost looks like you want to do an automatic
> log/archiving of image files generated.  If so, I also have the
> R.archive package (not yet on CRAN) in development.  All you need to
> do is load that package and everything else will be automatic.
> Whenever R.devices creates an image file (e.g. via toPNG()), a copy of
> it will be saved to ~/.Rarchive/%Y%m%d/%H%M%OS3-<imgname>.<ext>, e.g.
> ~/.Rarchive/2013-05-26/100330.684_GaussianDensity.png.  For every
> toPNG(), toPDF() etc another copy will be created with a unique
> filename.  That is useful when you do lots to EDA and want to go back
> to that image you did a couple of hours ago.  If this is what you
> want, let me know and I'll show you how to get access to R.archive.
>
>
> On Sun, May 26, 2013 at 8:20 AM, Vishal Belsare <shoot.spam at gmail.com> wrote:
>> Hi,
>>
>> Is it possible to :
>>
>> [1] set a default location to plot graphs in png format with specific
>> dimensions & resolution. I want to plot to a directory which is a shared on
>> the network (samba share), so as to view the plots from a different machine.
>> prior2plot <- function() {plotfile <-  paste('/srv/samba/share/Rplot-',
>> as.character(format(Sys.time(), "%Y%m%d-%H%M%S")), '.png', sep='');
>> png(filename=plotfile, width=1280, height=800)}
>
>>
>> [2] call dev.off() 'automagically' after a call to the plot function, by
>> (somehow) setting it as a default behavior in .Rprofile.site? This would be
>> nice to have, so as to update an image viewer running on a local machine
>> which keeps displaying the image(s) in the shared plot folder on the remote
>> machine (which runs R)
>>
>> I was thinking on the lines of adding the following to .Rprofile.site :
>> __________
>>
>> prior2plot <- function() {plotfile <-  paste('/srv/samba/share/Rplot-',
>> as.character(format(Sys.time(), "%Y%m%d-%H%M%S")), '.png', sep='');
>> png(filename=plotfile, width=1280, height=800)}
>>
>> setHook("before.plot.new", prior2plot())
>>
>> __________
>>
>> However, the above does not seem to work beyond a first plot.
>>
>> Best wishes,
>>
>> Vishal
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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.



More information about the R-help mailing list