'Why does grid::grid.grabExpr try to open an X11 connection, and how can I prevent it?

  1. On Linux, run DISPLAY= R, then issue grid::grid.grabExpr(1). This works fine, so grid::grid.grabExpr does not need an X11 connection.

  2. Run DISPLAY=XYZ R and issue the same command. I am now getting

    Error in .External2(C_X11, d$display, d$width, d$height, d$pointsize,  :
      unable to start device X11cairo
    In addition: Warning message:
    In (function (display = "", width, height, pointsize, gamma, bg,  :
      unable to open connection to X11 display ''
    Calls: <Anonymous> -> dev.set -> <Anonymous>
    

Why does grid::grid.grabExpr try to open an X11 connection when it does not need one?

Context: some R code which absolutely should not depend on X11 connection fails to run when I shutdown my X11 server, and I would like to make the code robust towards this happening.

Bonus question: Run DISPLAY=XYZ R, then issue Sys.unsetenv("DISPLAY"); grid::grid.grabExpr(1). I am getting the same error. The only difference between 1. and 2. being set variable DISPLAY, why does unsetting DISPLAY not take me back to the result of 1?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source