Knitting Rmd via GitHub actions doesn't create floating table of contents

Subject: Rendering Rmd via GitHub actions does not create floating table of contents

I’m trying to use GitHub Actions to render R Markdown (.Rmd) files to .html and .md. I’d like the .html file to have a floating table of contents. When I knit the .Rmd file locally, the .html file includes a floating table of contents. However, when I render the document to .html with GitHub Actions, the .html does not include a floating table of contents.

Here is my YAML header:

---
title: "INSERT TITLE"
author: "INSERT AUTHOR NAME"
date: '`r format(Sys.Date(), "%B %d, %Y")`'
output: 
  html_document:
    toc: true
    toc_float: true
    number_sections: true
---

Here is the render_markdown.yml file:

on:
  push:
    paths: ['**.Rmd']

name: render-rmarkdown

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

      - name: Set up pandoc
        uses: r-lib/actions/setup-pandoc@v2

      - name: Set up R
        uses: r-lib/actions/setup-r@v2

      - name: Install package dependencies
        uses: r-lib/actions/setup-r-dependencies@v2
      
      - name: Render Rmarkdown files and Commit Results
        run: |
          RMD_PATH=($(git diff --name-only ${{ github.event.before }} ${{ github.sha }} | grep '[.]Rmd$'))
          Rscript -e 'for (f in commandArgs(TRUE)) if (file.exists(f)) rmarkdown::render(f, output_format = c("html_document","github_document"))' ${RMD_PATH[*]}
          git config --local user.name "$GITHUB_ACTOR"
          git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com"
          git add *[.Rmd/.md]
          git add *[.Rmd/.html]
          git commit -m 'Re-build Rmarkdown files' || echo "No changes to commit"
          git push origin || echo "No changes to commit"

For a full reproducible example, you can see the GitHub repo here: https://github.com/isaactpetersen/markdowntest

Can you try rendering github_document then html_document to see if this makes a difference ?

Or can you set html_preview = FALSE for github_document ?

output:
    github_document:
        html_preview: FALSE

I think the HTML preview will override by default the HTML document you rendered before as they will have the same name. Changing the order should solve it, but setting the option too.

If you are generating a HTML document and md document yourself, you usually don't need the preview.

That answered my question perfectly. Changing the order solved the problem. I didn't realize that github_document was creating an HTML preview. Can you specify both outputs simultaneously in the same YAML header? So something like the following?:

---
title: "INSERT TITLE"
author: "INSERT AUTHOR NAME"
date: '`r format(Sys.Date(), "%B %d, %Y")`'
output: 
  html_document:
    toc: true
    toc_float: true
    number_sections: true
  github_document:
        html_preview: FALSE
---
1 Like

Yes exactly, just fix the indentation for the html_preview line.

When using render(..., output_format = "github_document"), the correct part of the YAML will be used, with the option defined.

Perfect, thank you! I greatly appreciate the help.

1 Like

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.