Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2010/05/17

Generating PNGs or JPGs via crontab in R

Dealing with R is one of the requirements of the position I have here in the shower, and here is a long-lasting complaint. We get data that we want to plot. R is wonderful for that. Here's a bit of example code.

temp <- c(63 , 63 , 63 , 63 , 63 , 63 , 63 , 63 , 63 , 63 , 63 , 63 , 63 , 63 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 65 , 64 , 64 , 63 , 63 , 63 , 63 , 63 , 63 , 63 , 63 , 63 , 63 , 63 , 62 , 62 , 62 , 62 , 61 , 61 , 61 , 61 , 61 , 61 , 61 , 61 , 61 , 61 , 61 , 61 , 59 , 59 , 59 , 59 , 59 , 59 , 58 , 58 , 58 , 58 , 58 , 58 , 58 , 58 , 58 , 58 , 58 , 58 , 58 , 58 , 58 , 58 , 58 , 57 , 56 , 56 , 56 , 56 , 56 , 56 , 56 , 56 , 56 , 56 , 56 , 56 , 55 , 55 , 55 , 55 , 55 , 55 , 55 , 55 , 54 , 54 , 54 , 53 , 53 , 53 , 53 , 53 , 53 , 52 , 52 , 52 , 52 , 52 , 52 , 52 , 52 , 52 , 53 , 52 , 52 , 52 , 52 , 51 , 51 , 51 , 51 , 51 , 51 , 51 , 51 , 51 , 51 , 51 , 51 , 51 , 51 )
png("/home/jacoby/test_weather.png")
plot( temp , type="l" )
dev.off()
That's a big, ugly code block, isn't it? That is actually supposed to be a graph of the weather info for the last 24 hours, taken from the Google Weather API in 20-minute intervals, but it mimics what I want graphed. There's some text shoved into the real example, but no big. In the real example, I have the template in a chunk of Perl code, which Perl fills with the correct values and then runs it as a shell command, which looks a bit like this.
R CMD BATCH /home/jacoby/weather.R 
When I run it by command line, that works perfect. I get a pretty PNG. Actually pretty ugly, but I could learn more formatting and pretty it up. But, when I run it via command line, we get this for the weather.Rout.
R version 2.5.0 (2007-04-23)
Copyright (C) 2007 The R Foundation for Statistical Computing
ISBN 3-900051-07-0

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

[Previously saved workspace restored]

> temp <- c(63 , 63 , 63 , 63 , 63 , 63 , 63 , 63 , 63 , 63 , 63 , 63 , 63 , 63 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 66 , 65 , 64 , 64 , 63 , 63 , 63 , 63 , 63 , 63 , 63 , 63 , 63 , 63 , 63 , 62 , 62 , 62 , 62 , 61 , 61 , 61 , 61 , 61 , 61 , 61 , 61 , 61 , 61 , 61 , 61 , 59 , 59 , 59 , 59 , 59 , 59 , 58 , 58 , 58 , 58 , 58 , 58 , 58 , 58 , 58 , 58 , 58 , 58 , 58 , 58 , 58 , 58 , 58 , 57 , 56 , 56 , 56 , 56 , 56 , 56 , 56 , 56 , 56 , 56 , 56 , 56 , 55 , 55 , 55 , 55 , 55 , 55 , 55 , 55 , 54 , 54 , 54 , 53 , 53 , 53 , 53 , 53 , 53 , 52 , 52 , 52 , 52 , 52 , 52 , 52 , 52 , 52 , 53 , 52 , 52 , 52 , 52 , 51 , 51 , 51 , 51 , 51 , 51 , 51 , 51 , 51 , 51 , 51 , 51 , 51 , 51 )
> png("/home/jacoby/test_weather.png")
Error in X11(paste("png::", filename, sep = ""), width, height, pointsize,  : 
 unable to start device PNG
In addition: Warning message:
unable to open connection to X11 display '' 
Execution halted
Notice how it's calling X11? If I run it via command line, it has access to X. If I run it via crontab, it doesn't. Which makes me wonder what sort of idiot decided to use that library for PNG generation. But that's not my core wonder. My core wonder is, is there a workaround? I mean, one better than our current "generate Postscript and use ps2jpeg to convert? Because for text and lines, for graphs, really, PNG is better than JPEG, and generating the right thing is better than several layers of conversion.

Yeah, it's a fairly ancient version of R. I think current is 2.11. It runs on a big Sun machine shared by several people whose grants pay for it and such, and if it isn't broken, think twice about an upgrade, right? I'd be hesitant to beg for an upgrade unless I was positive this was the fix.

Shub-Internet, tell me, is there an easy way to generate PNGs in R without having to use the X11 library?

2 comments:

  1. You can use a virtual framebuffer to make programs that really, really want X, on machines without it, happy.

    Debian(based distributions) have a nice wrapper-script, xvfb-run, that you just stick in front of your command.

    The package is called xvfb.

    If you find a solution that doesn't depend on xvfb, I'd be very interested.

    (I thought the Cairo-backend for R would work, but I haven't had any luck, yet).

    ReplyDelete
  2. Problem is that this is on Solaris.

    If it was Linux, a simple apt-get install would get me to a happy place. Alas and alack.

    But if I need the hack, good to know you know it. Thanks!

    ReplyDelete