Home > front end >  How to detect whether there's an OpenGL device in R?
How to detect whether there's an OpenGL device in R?

Time:12-28

I'm running R CMD CHECK via a Github action for the package I'm currently writing. It is ran on a Mac platform which does not have an OpenGL device, so R CMD CHECK fails because I run the rgl package in the examples. I think this will not be a problem for CRAN when I'll submit the package, I believe the CRAN platforms all have an OpenGL device, but I would like that R CMD CHECK works with the Github action. How could one detect whether there's an OpenGL device? If this is possible, I would change my examples to

if(there_is_openGL){
  library(rgl)
  ......
} 

EDIT

Thanks to user2554330's answer, I found the solution. One has to set the environment variable RGL_USE_NULL=TRUE in the yaml file. Environment variables are defined in the env section. My yaml file is as follows (in fact this is an Ubuntu platform, not a Mac platform):

on:
  push:
    branches: [main, master]
  pull_request:
    branches: [main, master]

name: R-CMD-check

jobs:
  R-CMD-check:
    runs-on: ubuntu-latest
    env:
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
      R_KEEP_PKG_SOURCE: yes
      RGL_USE_NULL: TRUE
    steps:
      - uses: actions/checkout@v2

      - uses: r-lib/actions/setup-r@v1
        with:
          use-public-rspm: true

      - uses: r-lib/actions/setup-r-dependencies@v1
        with:
          extra-packages: rcmdcheck

      - uses: r-lib/actions/check-r-package@v1

CodePudding user response:

I think it's hard to do what you want, because there are several ways rgl initialization can fail: you may not have X11 available, X11 may not support OpenGL on the display you have configured, etc.

If you are always running tests on the same machine, you can probably figure out where it fails and detect that in some other way, but it's easier to tell rgl not to attempt to use OpenGL before loading it.

For testing, the easiest way to do this is to set an environment variable RGL_USE_NULL=TRUE before running R, or from within R before attempting to load rgl. Within an R session you can use options(rgl.useNULL = TRUE) before loading rgl for the same result.

When rgl is not using OpenGL you can still produce displays in a browser using rglwidget(), and there are ways for displays to be updated automatically, which might be useful in RStudio or similar GUIs: use options(rgl.printRglwidget = TRUE, rgl.useNULL = TRUE).

  • Related