2

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
MCP_infiltrator
  • 3,961
  • 10
  • 45
  • 82

1 Answers1

0

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
starja
  • 9,887
  • 1
  • 13
  • 28
  • I will test it out and let you know. Thank you – MCP_infiltrator Jun 15 '22 at 13:00
  • Did it work or does it need some tweaks? If yes it would be great if you accept the answer – starja Jun 20 '22 at 13:48
  • 1
    Not it did not work, I apologize for not getting back to you. Here is the error: ```bash Run echo "Rendering the page..." Rendering the page... Error: Error in loadNamespace(x) : there is no package called ‘rmarkdown’ Calls: loadNamespace -> withRestarts -> withOneRestart -> doWithOneRestart Execution halted Error: Process completed with exit code 1. ``` – MCP_infiltrator Jun 20 '22 at 14:57
  • Thanks for your feedback. Do you use `renv` for this repository and is `rmarkdown` contained in the lockfile? – starja Jun 20 '22 at 15:21
  • I personally do not use renv for this repo – MCP_infiltrator Jun 20 '22 at 19:14
  • As I've written in my answer, it depends on `renv` in order to cache the dependencies (and making your workflow run faster after the first time). However, I've also added a version without `renv`. – starja Jun 21 '22 at 18:20
  • I’ll give it a try tomorrow – MCP_infiltrator Jun 22 '22 at 12:39
  • must be something else entirely going on, with and without renv, (renv like rlib says) still get failures. – MCP_infiltrator Jun 27 '22 at 13:21