Why is R having trouble rendering plots to a PNG file?

6

1

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.

jake9115

Posted 2013-06-04T20:05:40.380

Reputation: 1 029

If you will recompile R, be sure to install these packages in debian-based systems: xorg-dev libpango1.0-dev libcairo2-dev – Jefferson – 2016-08-26T19:47:25.180

Are you doing this remotely over SSH? – Breakthrough – 2013-06-04T20:08:13.150

Yes, I am SSHing into a cluster – jake9115 – 2013-06-04T20:12:30.840

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 – 2013-06-04T20:18:27.037

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 – 2013-06-04T20:20:54.807

1

Can you add the output of calling the capabilities() function in R? See this Stack Overflow question (and more specifically, 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 – 2013-06-04T20:26:02.197

ockquote>

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 – 2013-06-04T20:28:05.057

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 – 2013-06-04T20:29:12.353

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 – 2013-06-04T20:35:41.030

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 – 2013-06-04T20:46:55.363

@Breakthrough, jake, could one of you write this up as an answer? – terdon – 2013-06-05T14:44:45.667

@terdon done. I suppose I should have written it up as an answer, thank you for the reminder :) – Breakthrough – 2013-06-05T15:08:31.467

Answers

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

Posted 2013-06-04T20:05:40.380

Reputation: 32 927