Rendering both PDF and HTML is only possible interactively, not via script



This is a crosspost of my GitHub issue on the kableExtra repository, where I have yet to receive any feedback, so I thought I'd try here.

I have some Rmd documents (weekly assignments) I want to render both as PDF (for turning them in) and HTML (for sharing them with classmates), and to make it easier, I use a simple build script that calls rmarkdown:render() for both formats:

#!/usr/bin/env Rscript

rmarkdown::render(input = "testing.Rmd", output_format = "html_document")
rmarkdown::render(input = "testing.Rmd", output_format = "pdf_document")

(I am assuming that output_format = "all" would be the neater option, but due to this issue here I went with the more debugging-friendly version)

However, when used in a script, the commands yield an error when building the PDF:

Error: Functions that produce HTML output found in document targeting latex output.
Please change the output type of this document to HTML. Alternatively, you can allow
HTML output in non-HTML formats by adding this option to the YAML front-matter of
your rmarkdown file:

  always_allow_html: yes

Note however that the HTML output will not be visible in non-HTML formats.

Since it is possible to render both PDF and HTML interactively in RStudio through the "knit" button, I am confused as to what exactly I'm doing wrong. Also, the kableExtra author previously mentioned on Stackoverflow that kabelExtra functions are "smart" enough to decide for themselves which output format is required.
It appears this issue may call that into question, or I'm just doing something wrong.

To Reproduce

I have prepared a demo project here:
You can clone the project, interactively render the testing.Rmd to PDF and HTML, and then execute the lines in the build.R script which should yield the described error.

I would like to know if this is some bug somewhere or if my understanding of the knitr/RMarkdown/kableExtra internals is merely insufficient.

Tested this on macOS Mojave and Ubuntu 16.04, each with current R 3.5.1 and up to date packages (CRAN)