Runing R in a bash script

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Runing R in a bash script

Ulrik Stervbo-2
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
RSCRIPT

Thanks
Ulrik

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Runing R in a bash script

Jeffrey Horner
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
 > RSCRIPT

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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Runing R in a bash script

Henrik Bengtsson
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().

/Henrik

On 4/17/07, Jeffrey Horner <[hidden email]> 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
>  > RSCRIPT
>
> 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
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Runing R in a bash script

Ulrik Stervbo-2
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.

Thanks for the help
Ulrik

On 18/04/07, Henrik Bengtsson <[hidden email]> 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().
>
> /Henrik
>
> On 4/17/07, Jeffrey Horner <[hidden email]> 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
> >  > RSCRIPT
> >
> > 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
> >
> > ______________________________________________
> > [hidden email] 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.
> >
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Runing R in a bash script

Prof Brian Ripley
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).

> Thanks for the help
> Ulrik
>
> On 18/04/07, Henrik Bengtsson <[hidden email]> 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 <[hidden email]> 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
>>> > RSCRIPT
>>>
>>> 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
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>>>
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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,                  [hidden email]
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Runing R in a bash script

Jeffrey Horner
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
correspondingly.

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.

Jeff

>
>> Thanks for the help
>> Ulrik
>>
>> On 18/04/07, Henrik Bengtsson <[hidden email]> 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 <[hidden email]> 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
>>>>> RSCRIPT
>>>> 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

______________________________________________
[hidden email] 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.