Search code examples
rlinuxubuntujupyter-notebook

R kernel constantly crashes in Jupyter Notebooks due to graphics


I'm running R 4.3.2 on a Ubuntu 22.04 remote machine. I use vscode to SHH into it for all coding purposes except for R, as every time I run a Jupyter Notebook and execute code, the R kernel crashes immediately. The log says:

 unable to start device PNG
Calls: <Anonymous> ... evaluate -> dev.new -> do.call -> <Anonymous> -> ok_device
In addition: Warning message:
In ok_device(filename, ...) : unable to open connection to X11 display ''
Execution halted

which naturally led me to this post, so as recommended, I've tried switching

options(bitmapType='cairo')

in my ~/.Rprofile. This doesn't work either, but the logs now says:

13:52:48.798 [warn] StdErr from Kernel Process Error in dev.control(displaylist = "enable") : 
  dev.control() called without an open graphics device
Calls: <Anonymous> ... tryCatch -> tryCatchList -> evaluate -> dev.control
In addition: Warning message:
In ok_device(filename, ...) :
13:52:48.798 [warn] StdErr from Kernel Process type = "cairo" is unavailable. trying "Xlib"
Execution halted

Running R in the terminal seems fine, until I try to talk to the graphics by png():

> png()
Error in .External2(C_X11, paste0("png::", filename), g$width, g$height,  : 
  unable to start device PNG
In addition: Warning message:
In png() : could not open PNG file 'Rplot001.png'

I've also made sure to install

sudo apt install libpng-dev
sudo apt install libcairo2-dev
sudo apt install libjpeg-dev

I think my $LD_LIBRARY_PATHis also set correctly:

/usr/lib/x86_64-linux-gnu/libpng.so:/usr/lib/x86_64-linux-gnu/libcairo.so:/usr/lib/x86_64-linux-gnu/libpng.so:/usr/lib/x86_64-linux-gnu/libcairo.so:

What am I missing? Thanks

EDIT: Result of capabilities():

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

Solution

  • For the afterworld, this is how I solved it:

    I checked capabilities() as @M.Viking suggested in the comments and saw that PNG, but also cairo and X11 were FALSE, which is odd, as I've manually installed them.

    I realized that my R dist probably was compiled without these capabilities, for some odd reason. I thus re-compiled and reinstalled R like this:

    #  getting R 4.3.2 from CRAN
    wget https://cran.r-project.org/src/base/R-4/R-4.3.2.tar.gz
    tar -xzvf R-4.3.2.tar.gz
    cd R-4.3.2
    
    # THIS is the crucial config part
    ./configure --with-x=yes --with-libpng=yes --enable-R-shlib
    
    make
    sudo make install
    

    However, the error first persisted as X11 was still set to FALSE so I followed the common advice and switched to cairo by appending:

    options(bitmapType='cairo')
    

    to ~/.Rprofile (or wherever your .Rprofile lives)

    This did the trick :)