rrgl

Image not showing up - rgl


A year ago the following code used to show an output, but not it doesn't. No error is shown either. I'm reading the package changes but can't identify the reason.

library(rgl)

x1 <- seq(0, 2, len = 20)
x2 <- seq(0, 2, len = 20)
y <- outer(x1,x2, function(x1,x2) 1 + x1*2 + x2) 

persp3d(x=x1, y=x2, z=y, theta = 30, phi = 30, expand = 1, col = "green")
surface3d(x=x1, y=x2, z=y, back = "lines")
surface3d(x=x1, y=x2, z=y, front = "lines")
bgplot3d({
  plot.new()
  title(main = 'y = 1 + 2*x1 + x2', line = 3)
})

Solution

  • I tried to reproduce your problem and ran into the same problem myself. The issue here is that rgl is defaulting to using the "null" device ... and when I tried to explicitly stop that,

    open3d(useNULL=F)
    # Error in open3d(useNULL = F) : OpenGL is not available in this build
    

    This led me to how it was installed this time, and I found in the installation log (on the R console):

    install.packages("rgl")
    # Installing package into ‘/home/r2/R/x86_64-pc-linux-gnu-library/4.2’
    # (as ‘lib’ is unspecified)
    # trying URL 'https://packagemanager.posit.co/cran/latest/src/contrib/rgl_1.2.1.tar.gz'
    # Content type 'binary/octet-stream' length 2558042 bytes (2.4 MB)
    # ==================================================
    # downloaded 2.4 MB
    # * installing *source* package ‘rgl’ ...
    # ** package ‘rgl’ successfully unpacked and MD5 sums checked
    # ** using staged installation
    # checking for gcc... gcc
    # ...
    # configure: using libpng-config
    # configure: using libpng dynamic linkage
    # checking for X... no
    # configure: WARNING: X11 not found, continuing without OpenGL support.
    # configure: compiling without OpenGL support
    # configure: creating ./config.status
    

    Notably the compiling without OpenGL support part. (I'm running ubuntu-23.04 using wayland, if it matters.)

    Looking at the installation instructions, it mentions the OS packages libgl1-mesa-dev and libglu1-mesa-dev, and lo-and-behold they were not installed on my system. On a shell (not R) prompt, I confirmed this absence with:

    $ dpkg -l libgl1-mesa-dev libglu1-mesa-dev
    dpkg-query: no packages found matching libgl1-mesa-dev
    dpkg-query: no packages found matching libglu1-mesa-dev
    

    After installing them with:

    $ sudo apt install -y libgl1-mesa-dev libglu1-mesa-dev
    

    (much output), I then uninstalled the R package rgl and compiled it again from scratch.

    remove.packages("rgl")
    install.packages("rgl")
    # Installing package into ‘/home/r2/R/x86_64-pc-linux-gnu-library/4.2’
    # (as ‘lib’ is unspecified)
    # trying URL 'https://packagemanager.posit.co/cran/latest/src/contrib/rgl_1.2.1.tar.gz'
    # Content type 'binary/octet-stream' length 2558042 bytes (2.4 MB)
    # ==================================================
    # downloaded 2.4 MB
    # * installing *source* package ‘rgl’ ...
    # ** package ‘rgl’ successfully unpacked and MD5 sums checked
    # ** using staged installation
    # checking for gcc... gcc
    # ...
    # checking for libpng-config... yes
    # configure: using libpng-config
    # configure: using libpng dynamic linkage
    # checking for X... libraries , headers              <--- BETTER!
    # checking for XAllocClassHint... yes
    # checking for GL/gl.h... yes                        <--- another good sign
    # checking for GL/glu.h... yes
    # ...
    # configure: creating ./config.status
    

    noting subjectively that it took longer because it had more files to compile and link. Once that finished and I restarted R (necessary, I think),

    library(rgl)
    open3d()
    # Warning in par3d(userMatrix = c(1, 0, 0, 0, 0, 0.342020143325668, -0.939692620785909,  :
    #   font family "sans" not found, using "bitmap"
    # glX 
    #   1 
    x1 <- seq(0, 2, len = 20)
    x2 <- seq(0, 2, len = 20)
    y <- outer(x1,x2, function(x1,x2) 1 + x1*2 + x2) 
    
    persp3d(x=x1, y=x2, z=y, theta = 30, phi = 30, expand = 1, col = "green")
    surface3d(x=x1, y=x2, z=y, back = "lines")
    surface3d(x=x1, y=x2, z=y, front = "lines")
    bgplot3d({
      plot.new()
      title(main = 'y = 1 + 2*x1 + x2', line = 3)
    })
    

    produced a plot:

    basic rgl plot