Running an R script with Github actions

Daily data refresh

git
R
Github
Author

Nick Twort

Published

02 Aug 2023

If you want to run an R script every n days, use the below workflow file. Pop it in .github/actions and Bob’s your uncle. You might need to enable write permissions for Actions to your repo.

on:
  schedule:
    - cron: '*/30 01 * * *'
  workflow_dispatch:

name: refresh-data

jobs:
  refresh-data:
    runs-on: macOS-latest
    env:
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
      R_KEEP_PKG_SOURCE: yes
    steps:
      - uses: actions/checkout@v2

      - uses: r-lib/actions/setup-r@v2
        with:
          use-public-rspm: true
          
      - name: Cache packages
        uses: actions/cache@v3
        with:
            path: cool_name
            key: ${{ runner.os }}-renv-
            restore-keys: |
                ${{ runner.os }}-renv-

      - name: Install vctrs
        run: Rscript -e 'install.packages(c("vctrs"))'

      - name: Install packages
        run: Rscript -e 'install.packages(c("any_packages_you_use"))'


      - name: Get data
        run: Rscript -e 'source("your_script.R", echo = TRUE)'
        
      - name: Commit
        run: |
          git config --global user.name 'Your name'
          git config --global user.email 'yourgithubusername@github.com'
          git add .
          git commit -m 'refreshing data' || echo "No changes to commit"
          git push || echo "No changes to commit"