16

I am working with a R script within a wider pipeline that seems to not work with some versions of Rscript but with others. The call fails due to being unable to connect to X11, which is understandable because this is on a server. But my local installation of Rscript is able to handle this fine?

My local installation is version 3.0.1, while the one that other users that are reporting this problem are on 3.0.2.

Here is a simple test case - first the .R file:

#!/usr/bin/env Rscript

capabilities()

png("abc")

dev.off()

Run with my local env:

-bash-4.1$ ./test.R
    jpeg      png     tiff    tcltk      X11     aqua http/ftp  sockets 
    TRUE     TRUE     TRUE     TRUE    FALSE    FALSE     TRUE     TRUE 
  libxml     fifo   cledit    iconv      NLS  profmem    cairo 
    TRUE     TRUE    FALSE     TRUE     TRUE    FALSE     TRUE 
null device 
          1 

Run on the installation of Rscript others are trying to use:

    jpeg      png     tiff    tcltk      X11     aqua http/ftp  sockets 
    TRUE     TRUE    FALSE     TRUE    FALSE    FALSE     TRUE     TRUE 
  libxml     fifo   cledit    iconv      NLS  profmem    cairo 
    TRUE     TRUE    FALSE     TRUE     TRUE    FALSE     TRUE 
Error in .External2(C_X11, paste("png::", filename, sep = ""), g$width,  : 
  unable to start device PNG
Calls: png
In addition: Warning message:
In png("abc") : unable to open connection to X11 display ''
Execution halted
Ian Fiddes
  • 2,821
  • 5
  • 29
  • 49
  • Possible duplicate of [How to run R scripts on servers without X11](https://stackoverflow.com/questions/13067751/how-to-run-r-scripts-on-servers-without-x11) – Garini Nov 06 '18 at 15:58

4 Answers4

32

In case anyone ever finds this on google, the solution is

png("abc", type="cairo")
Ian Fiddes
  • 2,821
  • 5
  • 29
  • 49
  • 2
    Just a quick note, this didn't work for me, but i found another solution: Use the much more forgiving `bitmap()` function. Despite the name, it also renders flawless png's and does not need any X or similar. The reason probably has something to do with my setup; I am running RStudio Server on a headless VM. I did install X to render RGL output to a WebGL file, but didn't exactly succeed and then found the shinyRGL package, so i had no use for X anymore and removed it. Somehow, this seems to have confused R / the rendering device, and i get OP's error, independent of the `type` parameter. – zerweck Aug 26 '15 at 19:50
  • You can also use `options(bitmapType='cairo')`. I am having the same problem running shiny server on AWS ubuntu 14. I did this and the error went away, but still no plot. – Paul Jan 14 '17 at 20:24
  • I had to add `options(bitmapType='cairo')` to my `~/.Rprofile` – Shaun Jackman Jul 07 '17 at 00:58
  • I was receiving this error while my initial setup was `type="cairo"`, I had to bring down the resolution to make it work. `res=500` did not work and now `res=250, height=40, width=80` works – Ibo Nov 02 '18 at 23:05
14

You can run it in the R command

options(bitmapType='cairo')
png("xzvf.png")
plot(z~x)
dev.off()
Shicheng Guo
  • 1,233
  • 16
  • 19
4

Try prefixing the call to Rscript with

  xvfb-run

or even

 xvfb-run --server-args="-screen 0 1024x768x24"

as png, if memory serves, uses x11 font information. The virtual x11 server started by xvfb-run provides it, so it helps in headless settings, cron jobs, etc pp

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
0

I had the same issue on fresh install of Ubuntu 14.04.

Simply installing xvfb-run solved the issue, even without running it before launching R.

cmbarbu
  • 4,354
  • 25
  • 45