Run R RMD file with GitHub Actions

Viewed 45

I have two repositories that I update daily by simply opening the project in RStudio and knitting the RMD file, then pushing the results to the repo.

I would love to automate this with GitHub actions but I find the documentation highly confusing.

Is there an "Explain it like I'm 5" on doing this particular thing?

So I made a bash file located at ./bash/data_refresh.sh and a github actions file data_refresh.yaml

Bash file:

#!/bin/bash

echo "Rendering the page..."

Rscript -e "rmarkdown::render(input = 'README.Rmd')"

if [[ "$(git status --porcelain)" != "" ]]; then
    git config --global user.name 'my_user_name'
    git config --global user.email 'my_email_address'
    git add *
    git commit -m "Auto update Report"
    git push
fi

Here is my yaml:

# This is a basic workflow to help you get started with Actions

name: Package Data Refresh

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the "master" branch
  schedule:
    - cron: '0  12 * * *'

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    name: refresh the dashboard
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      - name: checkout_repo
        uses: actions/checkout@v2
        with:
          ref:  'master'
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - name: Render Rmarkdown
        run: bash ./bash/data_refresh.sh

I am getting the following error and not sure how to rectify it.

Run bash ./bash/data_refresh.sh
  bash ./bash/data_refresh.sh
  shell: /usr/bin/bash -e {0}
Rendering the page...
Error in loadNamespace(x) : there is no package called ‘rmarkdown’
Calls: loadNamespace -> withRestarts -> withOneRestart -> doWithOneRestart
Execution halted
1 Answers

You can take the workflows from r-lib as a starting point and add your parts (rendering & cron job).

This works when triggered manually, I haven't tested the cron job:

# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
  # Triggers the workflow on push or pull request events but only for the "master" branch
  schedule:
    - cron: '0  12 * * *'

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

name: Package Data Refresh

jobs:
  render-rmarkdown:
    runs-on: ubuntu-latest
    env:
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
    steps:
      - name: Checkout repo
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - uses: r-lib/actions/setup-pandoc@v2

      - uses: r-lib/actions/setup-r@v2

      - uses: r-lib/actions/setup-renv@v2
      
      - name: Render Rmarkdown Readme file and Commit Results
        run: |
          echo "Rendering the page..."
          Rscript -e 'rmarkdown::render(input = "README.Rmd")'
          if [[ "$(git status --porcelain)" != "" ]]; then
            git config --local user.name "$GITHUB_ACTOR"
            git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com"
            git add *
            git commit -m "Auto update Report"
            git push origin
          fi

This uses renv to install the dependencies and cache the dependencies in GHA (as far as I understand). Therefore, you need to use renv in your repository. Simply use renv::init() and commit the resulting files. As you use .Rmd, this includes rmarkdown. For more information, check the renv documentation.

The crucial part to make it work is that you have to use single quotes around the argument for Rscript, not double quotes. Otherwise it won't work.

Also, I'm quite new to GHA, so there might be other aspects you could optimise.

Edit

Here is a version that works without renv, however I recommend using renv as it allows to cache your dependencies (packages).

# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
  # Triggers the workflow on push or pull request events but only for the "master" branch
  schedule:
    - cron: '0  12 * * *'

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

name: Package Data Refresh 2

jobs:
  render-rmarkdown:
    runs-on: ubuntu-latest
    env:
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
    steps:
      - name: Checkout repo
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - uses: r-lib/actions/setup-pandoc@v2

      - uses: r-lib/actions/setup-r@v2
      
      - name: Install packages, render Rmarkdown Readme file and Commit Results
        run: |
          echo "Rendering the page..."
          Rscript -e 'install.packages(c("knitr", "rmarkdown"))'
          Rscript -e 'rmarkdown::render(input = "README.Rmd")'
          if [[ "$(git status --porcelain)" != "" ]]; then
            git config --local user.name "$GITHUB_ACTOR"
            git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com"
            git add *
            git commit -m "Auto update Report"
            git push origin
          fi
Related