# Graphics display issues with R built from source for RStudio Server

**URL:** <https://discourse.openondemand.org/t/graphics-display-issues-with-r-built-from-source-for-rstudio-server/1123>\
**Category:** Get Help\
**Tags:** question\
**Created:** [September 23, 2020, 1:22pm UTC](https://discourse.openondemand.org/t/graphics-display-issues-with-r-built-from-source-for-rstudio-server/1123 "2020-09-23T13:22:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ndusek](https://avatars.discourse-cdn.com/v4/letter/n/779978/32.png) [@ndusek](https://discourse.openondemand.org/u/ndusek)\
**Post date:** [September 23, 2020, 1:22pm UTC](https://discourse.openondemand.org/t/graphics-display-issues-with-r-built-from-source-for-rstudio-server/1123/1 "2020-09-23T13:22:36Z")

</div>

Hi All,

I am having some trouble getting plots to display in our RStudio Server interactive app. We are running this app without Singularity, in the same manner as [described in this post](https://discourse.openondemand.org/t/rstudio-without-singularity/773/13). The R interpreter we use for this, we built from source, but I suspect I’m missing some libraries, or compiler flags, or both. When we try to run a plot in RStudio Server, we get this error:

```nohighlight
Error in RStudioGD() : 
  Shadow graphics device error: r error 4 (R code execution error)
In addition: Warning message:
In grDevices:::png("/tmp/RtmpHm70aN/206c218b33c34010a3a833ac7bc55509.png", :
  unable to open connection to X11 display ''

```

This makes sense, because in a browser window, we wouldn’t have an X11 display device. Some searching around on the internet suggests that we may be missing `libcairo`, `libjpeg`, and possibly some others. I have tried building these, and other libraries, into our source build, but I am still unable to resolve this issue.

Does anyone have a recipe for building R from source so that it runs correctly within RStudio Server?

Thanks,  
Nick

---

<div class="post-metadata">

**Author:** ![jeff.ohrstrom](https://sea1.discourse-cdn.com/flex015/user_avatar/discourse.openondemand.org/jeff.ohrstrom/32/136_2.png) [@jeff.ohrstrom](https://discourse.openondemand.org/u/jeff.ohrstrom)\
**Post date:** [September 23, 2020, 2:06pm UTC](https://discourse.openondemand.org/t/graphics-display-issues-with-r-built-from-source-for-rstudio-server/1123/2 "2020-09-23T14:06:14Z")

</div>

Actually, I think it could be simpler than that. It seems to be taking issue with `/tmp/RtmpHm70aN` directory, that you don’t have write access to it?

[This is a similar Github issue on the RStudio repo](https://github.com/rstudio/rstudio/issues/2214).

---

<div class="post-metadata">

**Author:** ![ndusek](https://avatars.discourse-cdn.com/v4/letter/n/779978/32.png) [@ndusek](https://discourse.openondemand.org/u/ndusek)\
**Post date:** [September 23, 2020, 2:31pm UTC](https://discourse.openondemand.org/t/graphics-display-issues-with-r-built-from-source-for-rstudio-server/1123/3 "2020-09-23T14:31:22Z")

</div>

Actually, I do have write access to that directory, and to the .png image it’s complaining about. The issue – at least in my reading of the error message – seems to be RStudio’s inability to open the X11 connection.

[Here](https://community.rstudio.com/t/shiny-server-on-ubuntu-16-error-unable-to-open-connection-to-x11-display/11722/4) they recommend running `capabilities()` to check if support is available for various libraries. This is what I get:

```nohighlight
> capabilities()
       jpeg png tiff tcltk X11 aqua http/ftp sockets 
      FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE 
     libxml fifo cledit iconv NLS profmem cairo ICU 
       TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE 
long.double libcurl 
       TRUE TRUE 

```

The output indicates I’m missing support for all the major image types and X11 (among other things).

Nick

---

<div class="post-metadata">

**Author:** ![jeff.ohrstrom](https://sea1.discourse-cdn.com/flex015/user_avatar/discourse.openondemand.org/jeff.ohrstrom/32/136_2.png) [@jeff.ohrstrom](https://discourse.openondemand.org/u/jeff.ohrstrom)\
**Post date:** [September 23, 2020, 3:05pm UTC](https://discourse.openondemand.org/t/graphics-display-issues-with-r-built-from-source-for-rstudio-server/1123/4 "2020-09-23T15:05:59Z")

</div>

I see. OK. here are my capabilities. I think what you need is the `png` capability. Not sure about X11 but some stack overflow topics seem to indicate you need `cario` (you see I don’t have X11 but I do have cario).

```auto
> capabilities()
       jpeg png tiff tcltk X11 aqua http/ftp sockets libxml fifo 
       TRUE TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE TRUE 
     cledit iconv NLS profmem cairo ICU long.double libcurl 
       TRUE TRUE TRUE FALSE TRUE TRUE TRUE TRUE 

```

Can you run the `png()` function? I keep getting `NULL` even though I can link to an image, but I never get an error thrown.

```auto
> png()
> png('/tmp/foo')
> foo = png('/tmp/foo')
> png('/users/PZS0714/johrstrom/ondemand/dev/codeserver/icon.png')
> icon = png('/users/PZS0714/johrstrom/ondemand/dev/codeserver/icon.png')

```

---

<div class="post-metadata">

**Author:** ![ndusek](https://avatars.discourse-cdn.com/v4/letter/n/779978/32.png) [@ndusek](https://discourse.openondemand.org/u/ndusek)\
**Post date:** [September 23, 2020, 3:13pm UTC](https://discourse.openondemand.org/t/graphics-display-issues-with-r-built-from-source-for-rstudio-server/1123/5 "2020-09-23T15:13:37Z")

</div>

Here’s what I get when I run `png()`:

```nohighlight
Error in .External2(C_X11, paste0("png::", filename), g$width, g$height, : 
  unable to start device PNG
In addition: Warning message:
In png() : unable to open connection to X11 display ''

```

Re: Cairo, that’s the conclusion I came to as well. That’s why I was wondering if anyone had a recipe for an R source build I could try that has a working graphics component with RStudio Server.

How did you build your R installation?

---

<div class="post-metadata">

**Author:** ![maflister](https://sea1.discourse-cdn.com/flex015/user_avatar/discourse.openondemand.org/maflister/32/139_2.png) [@maflister](https://discourse.openondemand.org/u/maflister)\
**Post date:** [September 23, 2020, 3:32pm UTC](https://discourse.openondemand.org/t/graphics-display-issues-with-r-built-from-source-for-rstudio-server/1123/6 "2020-09-23T15:32:31Z")

</div>

We had the same issue a while back. It has to do with plotting in R on a headless server. If memory serves, the solution was to add `options(bitmapType='cairo')` to `/{RInstallDir}/lib64/R/etc/Rprofile.site`. You might need to create that file if you don’t already have it.

You also need the graphics capabilities. For building R at our site, we use:  
`./configure --prefix=/apps/R/4.0.0 --enable-R-shlib --with-x --with-cairo --with-jpeglib --with-readline --with-tcltk --with-blas --with-lapack --enable-R-profiling --enable-memory-profiling`

It’s been a while since we fixed this so your mileage may vary.

---

<div class="post-metadata">

**Author:** ![jasonbuechler](https://sea1.discourse-cdn.com/flex015/user_avatar/discourse.openondemand.org/jasonbuechler/32/302_2.png) [@jasonbuechler](https://discourse.openondemand.org/u/jasonbuechler)\
**Post date:** [January 11, 2021, 5:38pm UTC](https://discourse.openondemand.org/t/graphics-display-issues-with-r-built-from-source-for-rstudio-server/1123/7 "2021-01-11T17:38:32Z")

</div>

thank you @maflister, that was exactly what we needed!  
Can anyone weigh in here and explain _why_ that worked?

I speak about as much R as I speak Klingon, so it’s a minor miracle I found this unicorn of a forum post… I’ll provide a more complete scenario to help future searchers:

```nohighlight
## this is performed within the "app" 
## (before creating 'Rprofile.site' as suggested)
> require(ggplot2)
  Loading required package: ggplot2
> df <- data.frame(x=1:10,y=1:10)
> plot(df)
> ggplot(df,aes(x=x,y=y)) + geom_point()
> ggsave('/home/jtb49/tmp1.png')
  Saving 4.77 x 2.73 in image
  Error in .External2(C_X11, paste0("png::", filename), g$width, g$height, : 
    unable to start device PNG
  In addition: Warning message:
  In grDevices::png(..., res = dpi, units = "in") :
    unable to open connection to X11 display ''

```

And our environment after the `Rprofile.site` fix:

```nohighlight
$ module load R
$ RInstallDir=$(which R|sed 's/.bin.R$//')
$ cat $RInstallDir/lib64/R/etc/Rprofile.site
options(bitmapType='cairo')

## and just to verify cairo...
$ Rscript <(echo 'capabilities()')
       jpeg png tiff tcltk X11 aqua
       TRUE TRUE TRUE FALSE TRUE FALSE
   http/ftp sockets libxml fifo cledit iconv
       TRUE TRUE TRUE TRUE FALSE TRUE
        NLS profmem cairo ICU long.double libcurl
       TRUE FALSE TRUE TRUE TRUE TRUE

```

Thanks again!  
Jason Buechler  
High-Performance Computing  
Northern Arizona University

---

<div class="post-metadata">

**Author:** ![bennet](https://sea1.discourse-cdn.com/flex015/user_avatar/discourse.openondemand.org/bennet/32/333_2.png) [@bennet](https://discourse.openondemand.org/u/bennet)\
**Post date:** [January 11, 2021, 6:48pm UTC](https://discourse.openondemand.org/t/graphics-display-issues-with-r-built-from-source-for-rstudio-server/1123/8 "2021-01-11T18:48:57Z")

</div>

I believe that the default graphical device to which graphical output is sent is an X display, which rasterizes the imag. Using

options(bitmapType=‘cairo’)

instructs R to use the `cairo` rasterizer to create the plot instead of the X device. You might find something useful at

[https://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/Devices.html](https://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/Devices.html)

Just in case it comes up, users can change the bitmapType by including the `options()` command above with the desired type.

In the following snippet, I set the device to an invalid one, and R prints a list of what it knows about. Using any of the devices that work on Linux and don’t require a display should work (quartz is obviously OS X).

> require(ggplot2)  
> Loading required package: ggplot2  
> df ← data.frame(x=1:10,y=1:10)  
> plot(df)  
> ggplot(df,aes(x=x,y=y)) + geom\_point()

> options(bitmapType=‘x11’)  
> ggsave(‘/tmp/jtb49/foo.png’)  
> Saving 7 x 7 in image  
> Error in match.arg(type) :  
> ‘arg’ should be one of “cairo”, “cairo-png”, “Xlib”, “quartz”

> options(bitmapType=‘Xlib’)  
> ggsave(‘/tmp/foo.png’)  
> Saving 7 x 7 in image  
> Error in .External2(C\_X11, paste0(“png::”, filename), g$width, g$height, :  
> unable to start device PNG  
> In addition: Warning message:  
> In grDevices::png(…, res = dpi, units = “in”) :  
> unable to open connection to X11 display ‘’

> options(bitmapType=‘cairo-png’)  
> ggsave(‘/tmp/foo.png’)

Saving 7 x 7 in image

> options(bitmapType=‘cairo’)  
> ggsave(‘/tmp/foo.png’)  
> Saving 7 x 7 in image

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex015/uploads/osc/original/2X/b/bae70bd0ed39a3ae769c2108155f4cb3e9da8385.png) [@system](https://discourse.openondemand.org/u/system)\
**Post date:** [March 14, 2022, 5:45pm UTC](https://discourse.openondemand.org/t/graphics-display-issues-with-r-built-from-source-for-rstudio-server/1123/9 "2022-03-14T17:45:05Z")

</div>

Closed on March 14, 2022, 5:45pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
