6

I am trying to get R to do some very basic plotting and such in UNIX, but am getting a weirdo error relating to X11, when as far as I can tell I'm not even needing X11.

I have a matrix name d and want to save an image of a heatmap of this matrix without ever actually displaying the image (since I don't want to use X11). Here is my code:

png(file="my_image.png")
heatmap(d)
dev.off()

The problem is I am getting the following error:

Error in X11(paste("png::", filename, sep = ""), g$width, g$height, pointsize,  :
  unable to start device PNG

In addition: Warning message:

In png(file = "interative_hen.png") :
  unable to open connection to X11 display ''

I don't know this is happening, since I don't see how R is needing X11, and even if it does, X11 is installed and working properly for every application I tested it with.

Breakthrough
  • 34,227
  • 10
  • 105
  • 149
jake9115
  • 1,219
  • 4
  • 17
  • 24
  • Are you doing this remotely over SSH? – Breakthrough Jun 04 '13 at 20:08
  • Yes, I am SSHing into a cluster – jake9115 Jun 04 '13 at 20:12
  • I'm going to assume there's no X11 server on the cluster... If that's the case, you need to set an X11 server up on your computer, and configure your SSH client to allow X11 forwarding. This should allow you to generate the PNG files (some newsgroups mention that the X11 composter *is* required to render images from R). – Breakthrough Jun 04 '13 at 20:18
  • Thanks for the idea, but I already have an X11 server and client. From the same SSH window, I can type 'firefox' and fire up an X11 display of firefox, so I don't see why R is having a problem! – jake9115 Jun 04 '13 at 20:20
  • 1
    Can you add the output of calling the `capabilities()` function in R? See [this Stack Overflow question](http://stackoverflow.com/questions/1710853/how-to-run-r-on-a-server-without-x11-and-avoid-broken-dependencies) (and more specifically, [How to run R scripts on servers without X11](http://stackoverflow.com/questions/13067751/how-to-run-r-scripts-on-servers-without-x11)) regarding starting R with a virtual framebuffer. If you're comfortable, you might also want to try compiling R from source without X11 support but *with* PNG support (although I would try the virtual framebuffer first). – Breakthrough Jun 04 '13 at 20:26
  • > capabilities() jpeg png tiff tcltk X11 aqua http/ftp sockets FALSE FALSE FALSE TRUE FALSE FALSE TRUE TRUE libxml fifo cledit iconv NLS profmem cairo TRUE TRUE TRUE TRUE TRUE FALSE FALSE – jake9115 Jun 04 '13 at 20:28
  • it's difficult to tell from that formatting, but it appears as though you don't have PNG support (let me know if it said `TRUE` or `FALSE` underneath PNG). – Breakthrough Jun 04 '13 at 20:29
  • It said false. This makes more sense! I tried using the same code but substituting in 'pdf' for 'png' and it worked. Thanks for the 'capabilities()' hint! – jake9115 Jun 04 '13 at 20:35
  • Not a problem. If you *have* to run R on the cluster, you might have to recompile it yourself (on the cluster), and use your custom-compiled binary to parse & plot your dataset. This usually isn't too difficult, but it *does* assume that the cluster has all of the required build dependencies - and if it doesn't, installing them might be out of your control (depending if you have root/superuser access or not). If that's the case, it might be easier to download the data to your local machine and process the data there. – Breakthrough Jun 04 '13 at 20:46
  • @Breakthrough, jake, could one of you write this up as an answer? – terdon Jun 05 '13 at 14:44
  • @terdon done. I suppose I should have written it up as an answer, thank you for the reminder :) – Breakthrough Jun 05 '13 at 15:08
  • If you will recompile R, be sure to install these packages in debian-based systems: xorg-dev libpango1.0-dev libcairo2-dev – Jefferson Aug 26 '16 at 19:47

1 Answers1

5

First, check to see if the version of R you're using has PNG capabilities. You can do this by calling the capabilities() function from an R prompt. It should print out a list similar to:

> capabilities()

jpeg    png    tiff    tcltk  X11    aqua     http/ftp  sockets
FALSE   FALSE  FALSE   TRUE   FALSE  FALSE    TRUE      TRUE

libxml  fifo   cledit  iconv  NLS    profmem  cairo
TRUE    TRUE   TRUE    TRUE   TRUE   FALSE    FALSE 

If you see FALSE under png, then you need to manually recompile R with explicit PNG support. So long as you have installed the necessary build dependencies, the build process should automatically enable PNG capabilities.


Lastly, assuming there's no local X server running on the cluster, your SSH client might not be properly configured - specifically, ensure that you have enabled X11 forwarding (using the -X or -Y flags if you are using a UNIX-like ssh tool). Alternatively, you can try using a virtual framebuffer.

See this Stack Overflow question for details: How to run R on a server without X11, and avoid broken dependencies.

Breakthrough
  • 34,227
  • 10
  • 105
  • 149