[R] Runing R in a bash script

Jeffrey Horner jeff.horner at vanderbilt.edu
Wed Apr 18 16:36:03 CEST 2007

Prof Brian Ripley wrote:
> On Wed, 18 Apr 2007, Ulrik Stervbo wrote:
>> As I har problems installing the Cairo package, I went for Henriks solution
>> - and it works almost perfect. I would like to have been able to generate
>> transparent png.
> You cannot do transparency via postscript.
> I would suggest using pdf() and converting the output of that, which often 
> works even better (and does have full transparency support).

Cairo also has full transparency support, both for png and pdf.

Ulrik, I presume the problem you ran into was the absence of the cairo 
library dependency:

configure: error: Cannot find cairo.h! Please install cairo 
(http://www.cairographics.org/) and/or set CAIRO_CFLAGS/LIBS 

which has it's own library dependencies as well. Most all Linux 
distributions (I'm presuming you're running Linux) have ways to install 
these dependencies easily: apt-get, rpm, etc.

Otherwise, if you had other problems installing Cairo, please let me 
know and I can fix them.


>> Thanks for the help
>> Ulrik
>> On 18/04/07, Henrik Bengtsson <hb at stat.berkeley.edu> wrote:
>>> Or see png2() in R.utils, which imitates png() but uses bitmap(),
>>> which in turn uses postscript-to-png via ghostscript.  BTW, personally
>>> I think PNGs generated via bitmap() look way better than the ones
>>> generated via png().
> As there are two separate versions of png() for different OSes, comments 
> like that are very system-dependent.  Other postings suggest this is 
> Windows, and if png() is giving poor results there it suggests a problem 
> with the way Windows' GDI is configured (which depends on the graphics 
> card).
> And of course, PNGs don't 'look' at all: they are rendered by some other 
> tool, and quite often the perceived problem with R graphical output is in 
> fact with the rendering tool.
>>> /Henrik
>>> On 4/17/07, Jeffrey Horner <jeff.horner at vanderbilt.edu> wrote:
>>>> Ulrik Stervbo wrote:
>>>>> Hello!
>>>>> I am having issues trying to plot to a ong (or jpg)  when the R-code
>>> in a
>>>>> bash script is executed from cron.
>>>>> I can generate a pdf file, but when I try to write to a png, the file
>>> is
>>>>> created, but nothing is written. If I execute the bash script from my
>>>>> console, everything works file. Any ideas?
>>>>> In my cron I have SHELL=/bin/bash - otherwise /bin/shell is used and
>>> the
>>>>> folowing enery, so example is executed every minute
>>>>> * * * * * [path]/example.sh
>>>>> I am running
>>>>> R version 2.4.1 (2006-12-18)
>>>>> Here's a minimal example - two files one R-script ('example.r') and
>>> one
>>>>> bash-script ('example.sh')
>>>>> example.r
>>>>> # Example R-script
>>>>> x <- c(1:10)
>>>>> y <- x^2
>>>>> png(file="example2.png")
>>>>> #pdf(file="example2.pdf")
>>>>> plot(x,y)
>>>>> graphics.off()
>>>>> example.sh
>>>>> #/bin/bash
>>>>> #
>>>>> # Hello world is written to exhotext every time cron executes this
>>> script
>>>>> echo "Hello world" > echotext
>>>>> # This works, but not when executed from cron
>>>>> n=`R --save < example.r`
>>>>> # using exec as in `exec R --save < example.r` dosent work with cron
>>>> either
>>>>> # This also works, but nothing is written to the png when executed
>>>> from cron
>>>>> R --save <<RSCRIPT
>>>>> x <- c(1:10)
>>>>> y <- x^2
>>>>> png(file="example2.png")
>>>>> #pdf(file="example2.pdf")
>>>>> plot(x,y)
>>>>> graphics.off()
>>>>> #dev.off() dosent work at all when executed from cron
>>>> The png() device requires an X server for the image rendering. You might
>>>> be able to get away with exporting the DISPLAY environment variable
>>>> export DISPLAY=:0.0 # try and connect to X server on display 0.0
>>>> within your script, but it will only work if the script is executed by
>>>> the same user as is running the X server, *and* the X server is running
>>>> at the time the script is executed.
>>>> There are a handful of packages that will create a png without the
>>>> presence of an X server, and I'm partial to Cairo (since I've done some
>>>> work on it). You can install the latest version like this:
>>>> install.packages("Cairo",,'http://rforge.net/',type='source')
>>>> Cairo can also outputs nice pdf's with embedded fonts... useful if you
>>>> want to embed high-quality OpenType or TrueType fonts.
>>>> Best,
>>>> Jeff
>>>> --
>>>> http://biostat.mc.vanderbilt.edu/JeffreyHorner

More information about the R-help mailing list