2

I am running R 3.1.2 on a headless Raspberry Pi 2 with Raspbian. Packages install fine, but I can't save PNGs. After googling the error for a long time, I found this response, which says I need to "manually recompile R with explicit PNG support."

I apologize if this is a basic question, but how do I recompile R with PNG support? If it's helpful, this is how I installed in the first place. Thanks in advance!

Community
  • 1
  • 1
D.Hadley
  • 1,179
  • 13
  • 13

2 Answers2

3

What does capabilities()["png"] say? I suspect it says TRUE as I see no reason why your Raspian package should not follow the standard configuration of both the R source defaults as well as the Debian package default.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • 1
    Thanks for responding. `png` `jpg` `x11` and `cairo` are all `FALSE`. I remember getting an error while installing the 'jpeg' package, which I fixed by `sudo apt-get install libjpeg-dev`. Maybe that's a clue? – D.Hadley Mar 14 '15 at 23:41
  • 1
    Ok, now we have that confirmed. Come to think about it, this makes sense as R is _headless_ per your post, hence no X11 and hence no font metrics and no device. You can get png et al if you wrap a headless R inside of `xvfb-run` (if Raspian has that; see some other answers here). Otherwise try one of the add-on cairo device packages for headless plotting. – Dirk Eddelbuettel Mar 14 '15 at 23:44
  • Thanks, I will try `xvfb-run`. My ultimate goal is to use cron to call an Rscript that crunches data and outputs a plot. Is it still considered _headless_ if I call the Rscript through a timed cron job instead of manually through SSH? Would that make a difference? Again, I really appreciate the help. – D.Hadley Mar 14 '15 at 23:55
  • 1
    Yes, the definition of _headless_ is _no `DISPLAY` variable_ which is pretty much guaranteed under `cron`. But that is an almost-FAQ, now that you know what to look for you can get this fixed. It has noting to do with Raspian---you'd have the _exact same issue_ with an amd64 desktop or server. – Dirk Eddelbuettel Mar 14 '15 at 23:57
  • 1
    Also see [this earlier question](http://stackoverflow.com/questions/24999983/) with an alternative. – Dirk Eddelbuettel Mar 14 '15 at 23:58
  • thanks for answering my dumb question about cron. I tried `xvrb-run` with and without the server-args. I still get `Error in .External2(C_X11, paste("png::", filename, sep = ""), g$width, : unable to start device PNG` and `In grDevices::png(..., width = width, height = height, res = dpi, : could not open PNG file '/myfile.png'`. I'm guessing recompiling will not help. – D.Hadley Mar 15 '15 at 01:13
  • Err, `/myfile.png` will **never** work unless you are `root`. Did you mean `/tmp/myfile.png`? – Dirk Eddelbuettel Mar 15 '15 at 01:15
  • sorry, I just shortened the file path to make the comment easier to read (but added confusion). It's more like `/home/pi/Github/myrepository/myfile.png` – D.Hadley Mar 15 '15 at 01:24
1

Thanks to @Dirk, who correctly diagnosed this as a problem of running R headless on the pi, I found a solution by mixing answers from previous posts:

  • I start R by calling xvfb-run --server-args="-screen 0 1024x768x24" sudo R
  • I installed the Cairo package in R and save plots using code I adapted from this blog post

I also found this site helpful.

D.Hadley
  • 1,179
  • 13
  • 13