Max Chis
Max Chis

Reputation: 63

Caching GitHub Actions R Packages or otherwise speeding up installation?

I put together a Github Actions Workflow that installs R and R packages to the runner and then runs unit tests on the Github Repository.

The process currently works, but it's relatively slow, the workflow taking anywhere between 4 and 8 minutes, and the majority of that time occupied by the installation of R packages. I'm wondering if I'm doing something sub-optimally with my Github Action that could be improved to speed up the overall workflow. In particular, I'm wondering if I can cache the R or R package installation to speed up the process, but I haven't seen a lot of information on how that would work -- most talk of caching using Github Actions relates to other languages, like with npm.

Does anyone have any ideas?

name: Run R Tests
on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - uses: r-lib/actions/setup-r@v1
      with:
        r-version: '3.5.3' # The R version to download (if necessary) and use.
    - name: Install Packages
      run: Rscript -e 'install.packages(c("readr", "magrittr", "testthat", "here"))'
    - name: Get current directory
      run: pwd
    - name: Run testthat
      run: Rscript './run_tests.R'

Upvotes: 4

Views: 1267

Answers (2)

kraggle
kraggle

Reputation: 347

I believe next you would want to take advantage of r-lib/actions/setup-r-dependencies@v2 action, which will install all package dependencies defined in your DESCRIPTION file. setup-r-dependencies

This package features: Setting up a dependency cache using actions/cache. This should cut runtimes in half. Also I think the environment variable: R_COMPILE_AND_INSTALL_PACKAGES: never should be set which uses binaries.

Upvotes: 2

Dirk is no longer here
Dirk is no longer here

Reputation: 368261

"It all depends" on what you want to do but I described my setup in a few posts below this post (you may need to scroll a little for older posts). The most recent one gives a very good overview and intro, and has a video.

More posts below details RSPM and BSPM (my default). I am content to mostly just run on Linux (and sometimes on macOS) but my minimal test time is under two minutes per package as I rely heavily on pre-made .deb binaries that install fast and reliably. It can use the default actions for macOS too in simple cases (but I have seen macOS failures when source code needed a Fortran compiler and whatnot; I just skipped the macOS builds there).

A side benefit of what I use (and provide at the r-ci website is that it works the same way at other CI providers should one choose to (or be forced to) migrate once more as many of us did from Travis to GitHub Actions.

Upvotes: 1

Related Questions