Why does rendering a pdf from rmarkdown require closing rstudio between renders? - r

Background
I am trying to make a rmarkdown document that is rendered using render(). The render call has two elements that are parameterized:
I would like the user to be able to specify pdf or html.
Straightforward using output_format().
I also would like to pass a parameter to the document to specify if the tables (using the kableExtra package) are latex or html.
This is the rmarkdown file called test.Rmd
---
title: "Title"
author: "Zzz Zzzzzz"
params:
table_format:
value
---
```{r setup}
knitr::opts_chunk$set(echo = FALSE)
library(knitr)
library(kableExtra)
options(knitr.table.format = params$table_format)
```
## Test table
```{r cars}
if (params$table_format == "latex"){
kable(iris[1:100,], booktabs = T) %>%
kable_styling(latex_options = c("scale_down"))
}
if (params$table_format == "html"){
kable(iris[1:100,]) %>%
kable_styling(bootstrap_options = c("striped", "hover")) %>%
scroll_box(width = "500px", height = "600px")
}
params$table_format
```
Now here are the two calls to render the file:
rmarkdown::render("test.Rmd", output_format = "pdf_document", params = list(
table_format = "latex"
))
rmarkdown::render("test.Rmd", output_format = "html_document", params = list(
table_format = "html"
))
Problem
Now if I open up a fresh rstudio session I can run both render calls no problem. Either .pdf or an .html file is created. However if I then try to run the .pdf render again i get the following error:
"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS
test.utf8.md --to latex --from
markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash
--output test.pdf --template "C:\Users\salbers\R\win-library\3.4\rmarkdown\rmd\latex\default-1.17.0.2.tex"
--highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in" ! Undefined control
sequence. \begin {tabular}{rrrrl} \toprule
Sepal.Length & Sepal.Width & Pet... l.267 \end{tabular}}
pandoc.exe: Error producing PDF Error: pandoc document conversion
failed with error 43 In addition: Warning message: running command
'"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS
test.utf8.md --to latex --from
markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash
--output test.pdf --template "C:\Users\salbers\R\win-library\3.4\rmarkdown\rmd\latex\default-1.17.0.2.tex"
--highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in"' had status 43
The html render works fine over and over again. If I close rstudio, then ropen the project, the render for pdf work fine as well.
Question
Can anyone tell me why my pdf rendered rmarkdown document can't be rendered twice in a single session of rstudio?
Similarly, why does rstudio have to closed in between renderings?

2019-01-21 UPDATE:
One of the big difference between the knit button and the render function is that, the knit button always starts with a "new environment" (as we all can feel) while the render function by default starts in the parent.env().
render(input, ..., envir = parent.frame(), ...)
In the function documentation, we see
envir
The environment in which the code chunks are to be evaluated
during knitting (can use new.env() to guarantee an empty new
environment).
Therefore, we can simulate the behavior of clicking the knit button by putting envir = new.nev() in the render call.
Original Answer:
Hmm, let me post the solution first. To solve this behavior, you need to put the following stuff in your yaml section. I also added a function kableExtra_latex_packages() in the dev version earlier this week to bring up the following texts.
header-includes:
- \usepackage{booktabs}
- \usepackage{longtable}
- \usepackage{array}
- \usepackage{multirow}
- \usepackage[table]{xcolor}
- \usepackage{wrapfig}
- \usepackage{float}
- \usepackage{colortbl}
- \usepackage{pdflscape}
- \usepackage{tabu}
- \usepackage{threeparttable}
- \usepackage[normalem]{ulem}
If you are curious why there is such an odd behavior, here is a short explanation. When you first load kableExtra in a rmarkdown environement, it will try to put the LaTeX package information above into the rmarkdown metadata using the usepackage_latex() function that comes with this package. It works fine if you just hit the knit button because every "knit+rendering" process is supposed to be isolated. However, surprisingly (btw thanks for bringing it up), as we see it here, if you are trying to render from console, since (my assumption) knitr or rmarkdown is trying to reuse some cached results, this process failed to replicate. It turns out these LaTeX package dependencies were not put into the tex file being generated and ends up with an error. If you close RStudio and restart it, of course, the temporary memory it has will be gone and you should be able to load those packages again. I feel like it could be a global-variable-related bug in rmarkdown. I guess we can fix it by adding a "forget the meta" part at the end of the render function but I need to look it that.
Part of it was my bad for not providing enough documentations on LaTeX packages that were used in past releases. Now, I added a new section about this issue at the very beginning of the package vignette of kableExtra 0.5.0, which was released earlier this week. Feel free to check it out. Also, as I said earlier, in current dev version, you can bring up the list using the function kableExtra_latex_packages().

In my case, #Hao answer didn't work...I finally managed unloading the kableExtra package after each render execution, as follows:
rmarkdown::render('torender.Rmd')
detach("package:kableExtra", unload=TRUE)
It should be possible also selecting the environment using something like
rmarkdown::render('torender.Rmd',envir=new.env(some parameters))
which is cleaner....but I didn't manage this way!

I had a similar problem: a loop to render PDF reports that contained a table. PDF would not render with booktabs = T. My solution was to manually load all of the latex packages in my setup chunk in the Rmd script that was being called from the loop.
So in the Rmd script that was being called multiple time I have:
usepackage_latex("booktabs")
usepackage_latex("longtable")
usepackage_latex("array")
usepackage_latex("multirow")
usepackage_latex("xcolor", "table")
usepackage_latex("wrapfig")
usepackage_latex("float")
usepackage_latex("colortbl")
usepackage_latex("pdflscape")
usepackage_latex("tabu")
usepackage_latex("threeparttable")
usepackage_latex("threeparttablex")
usepackage_latex("ulem", "normalem")
usepackage_latex("makecell")
This fixed the issue - the PDFs rendered with formatted tables.
Hopefully, this helps someone.

I found an easy way to do this for multiple .Rmd files was to make a "headers.tex" file with the list of these kableExtra-added \usepackage{} commands. Then in the YAML header of the .Rmd file:
output:
pdf_document:
includes:
in_header: headers.tex
Here's what I put in the header.tex file -- I'd copied it from one that worked, and it's also the output of kableExtra_latex_packages() stripping off the "header-includes:" line and the dashes.
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{array}
\usepackage{multirow}
\usepackage{wrapfig}
\usepackage{float}
\usepackage{colortbl}
\usepackage{pdflscape}
\usepackage{tabu}
\usepackage{threeparttable}
\usepackage{threeparttablex}
\usepackage[normalem]{ulem}
\usepackage{makecell}
\usepackage{xcolor}

Related

LearnR Rmd knit to pdf failed to compile after update of MikTex

After I updated MikTex to the latest version, my Rmd files don't compile anymore. I installed tinytex, reinstalled miktex, R and RStudio several times. I use some newcommands like \C, \G etc. in a tex document which I include in my Rmd file. I use windows 10, the latest versions of R, RStudio and Miktex.
I asked a colleague (who had no compiling issues) to update his MikTex as well. After updating his MikTex, he had the same issue.
Here is the Rmd code:
---
documentclass: scrartcl
fontsize: 11pt
output:
pdf_document:
includes:
in_header: commands.tex
---
This is an example:
\begin{itemize}
\item hello
\item world
\end{itemize}
And here is the tex code:
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{icomma}
\usepackage[right,official]{eurosym}
\usepackage{hyperref}
\usepackage{booktabs}
\usepackage{graphicx}
\usepackage[shortlabels]{enumitem}
\usepackage{setspace}
\singlespacing
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Seitenformat und Layoutparameter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\hbadness=1000
\tolerance=10000
\voffset-2.80cm
\hoffset-2.60cm
\topmargin1.50cm
\headheight0.65cm
\headsep1.0cm
\topskip0cm
\textheight24.00cm
\footskip1.5cm
\footnotesep11pt
\evensidemargin2.50cm
\oddsidemargin2.50cm
\textwidth16cm
%\parindent0cm
%\parskip1.5ex plus0.5ex minus 0.5ex
% simple letters
\newcommand{\A}{{\mathbb A}}
\newcommand{\B}{{\mathbb B}}
\renewcommand{\C}{{\mathbb C}}
\newcommand{\D}{{\mathbb D}}
\newcommand{\E}{{\mathbb E}}
And I get:
processing file: example1.Rmd
|......................................................................| 100%
ordinary text without R code
"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS example1.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output example1.tex --lua-filter "C:\Users\saski\Documents\R\win-library\4.0\rmarkdown\rmarkdown\lua\pagebreak.lua" --lua-filter "C:\Users\saski\Documents\R\win-library\4.0\rmarkdown\rmarkdown\lua\latex-div.lua" --self-contained --highlight-style tango --pdf-engine pdflatex --include-in-header commands.tex --variable graphics
output file: example1.knit.md
! LaTeX Error: Command \C undefined.
Fehler: LaTeX failed to compile example1.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See example1.log for more info.
Ausführung angehalten
The best solution would be to avoid one letter macro names
your code no longer works because \C is now only defined by hyperref under certain circumstances, see https://tex.stackexchange.com/questions/582625/command-c-already-defined-and-the-hyperref-package/582630#582630 for more information
If you use \newcommand instead of \renewcommand you can see that the error vanishes
% simple letters
\newcommand{\A}{{\mathbb A}}
\newcommand{\B}{{\mathbb B}}
\newcommand{\C}{{\mathbb C}}
\newcommand{\D}{{\mathbb D}}
\newcommand{\E}{{\mathbb E}}
(but you should still not do that, use some other macro names instead if the one letter names)
Conclusion: don't use one letter macro names

rticles templates do not compile [duplicate]

Knitting (in RStudio version 1.2.1335) an RMarkdown file to PDF fails when trying to create citations (for pandoc version 2.8.0.1, and R version 3.6.1). (This does not happen when knitting to HTML, for example.)
Here is a small rep. ex. in RMarkdown:
---
title: "Rep. Ex. for 'LaTeX Error: Environment cslreferences undefined'"
output:
pdf_document: default
bibliography: report.bib
---
```{r generate-bibtex-file, include=FALSE}
knitr::write_bib(file = "report.bib", prefix = "")
```
# Used R version
R 3.6.1 [#base]
# References
Knitting this yields as final output (on my machine):
"C:/PROGRA~1/Pandoc/pandoc" +RTS -K512m -RTS RepEx.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output RepEx.tex --template "C:\Users\gcb7\Documents\R\win-library\3.6\rmarkdown\rmd\latex\default-1.17.0.2.tex" --highlight-style tango --pdf-engine pdflatex --variable graphics=yes --lua-filter "C:/Users/gcb7/Documents/R/win-library/3.6/rmarkdown/rmd/lua/pagebreak.lua" --lua-filter "C:/Users/gcb7/Documents/R/win-library/3.6/rmarkdown/rmd/lua/latex-div.lua" --variable "geometry:margin=1in" --variable "compact-title:yes" --filter "C:/PROGRA~1/Pandoc/pandoc-citeproc.exe"
output file: RepEx.knit.md
! LaTeX Error: Environment cslreferences undefined.
This seems to have started after a recent update to pandoc 2.8.0.1, and I just found on https://pandoc.org/releases.html that in 2.8 a few changes seem to have been made in the cslreferences environment (but up to now there seems to have nothing appeared on pandoc-discuss or on the respective github bug tracker).
Any ideas?
According to the release notes you linked, cslreferences was introduced in version 2.8, including a suitable definition of this environment in the pandoc template. However, Rmarkdown is using its own template (C:\Users\gcb7\Documents\R\win-library\3.6\rmarkdown\rmd\latex\default-1.17.0.2.tex in your case), which does not have this definition. This has been fixed on GitHub, c.f. https://github.com/rstudio/rmarkdown/issues/1649.
One workaround would be to copy the relevant lines to a local copy of Rmarkdown's template and specify that via the template field. Alternatively you could add
\newlength{\cslhangindent}
\setlength{\cslhangindent}{1.5em}
\newenvironment{cslreferences}%
{\setlength{\parindent}{0pt}%
\everypar{\setlength{\hangindent}{\cslhangindent}}\ignorespaces}%
{\par}
or
\newenvironment{cslreferences}%
{}%
{\par}
to the resulting tex file via header-includes or similar. Or you could use the pandoc that comes with RStudio, if you have that installed. This can be accomplished by prepending <rstudio-dir>/bin/pandoc/ to the PATH, possibly within .Renviron to make it R specific.
Everything untested, since I do not have pandoc 2.8 ...
Had the same issue when using thesisdown.
Which was confusing, since the solution from Ralf (adding \newenvironment{cslreferences} ) is already included in the template.tex file form thesisdown.
After some while I figured out:
Changing
\newenvironment{cslreferences}% to
\newenvironment{CSLReferences}% solves the problem.
Specifically if you are also having this problem with thesisdown, you must alter the template.tex file.
The section in template.tex should look like this then:
$if(csl-refs)$
\newlength{\cslhangindent}
\setlength{\cslhangindent}{1.5em}
\newenvironment{CSLReferences}%
{$if(csl-hanging-indent)$\setlength{\parindent}{0pt}%
\everypar{\setlength{\hangindent}{\cslhangindent}}\ignorespaces$endif$}%
{\par}
$endif$
As also described here.
Seems like the default Pandoc template also uses \newenvironment{CSLReferences} since Version 2.11 (see Commit)

Specifying multiple simultaneous output formats in knitr (new)

Can I write a YAML header to produce multiple output formats for an R Markdown file using knitr? I could not reproduce the functionality described in the answer for the original question with this title.
This markdown file:
---
title: "Multiple output formats"
output:
pdf_document: default
html_document:
keep_md: yes
---
# This document should be rendered as an html file and as a pdf file
produces a pdf file but no HTML file.
And this file:
---
title: "Multiple output formats"
output:
html_document:
keep_md: yes
pdf_document: default
---
# This document should be rendered as an html file and as a pdf file
produces an HTML file (and an md file) but no pdf file.
This latter example was the solution given to the original question. I have tried knitting with Shift-Ctrl-K and with the Knit button in RStudio, as well as calling rmarkdown::render, but only a single output format is created, regardless of the method I use to generate the output file.
Possibly related, but I could not identify solutions:
How do I produce R package vignettes in multiple formats?
Render all vignette formats #1051
knitr::pandoc can't create pdf and tex files with a single config #769
Multiple formats for pandoc #547
An allusion to multiple output format support in a three year old RStudio blog post
Using R version 3.3.1 (2016-06-21), knitr 1.14, Rmarkdown 1.3
I actually briefly mentioned in Render all vignette formats #1051 and you missed it:
rmarkdown::render('your.Rmd', output_format = 'all')
It is documented on the help page ?rmarkdown::render.
Notwithstanding Yihui Xie's authoritative answer, and with due respect to the author of a great package, there are many cases in which output_format = 'all' is sub-optimal.
One of the issues that this solution raises is that the R script is re-processed from scratch for each format. Proof:
rmarkdown::render("new.Rmd", output_format = c("html_document", "pdf_document"))
processing file: new.spin.Rmd
|....................... | 33%
ordinary text without R code
|............................................... | 67%
label: unnamed-chunk-1
|......................................................................| 100%
ordinary text without R code
output file: new.knit.md
"C:/Users/fabrn/AppData/Local/Pandoc/pandoc" +RTS -K512m -RTS new.utf8.md --to html4 --from markdown+autolink_bare_uris+tex_math_single_backslash --output new.html --lua-filter "C:\Users\fabrn\R\win-library\4.0\rmarkdown\rmarkdown\lua\pagebreak.lua" --lua-filter "C:\Users\fabrn\R\win-library\4.0\rmarkdown\rmarkdown\lua\latex-div.lua" --email-obfuscation none --self-contained --standalone --section-divs --template "C:\Users\fabrn\R\win-library\4.0\rmarkdown\rmd\h\default.html" --no-highlight --variable highlightjs=1 --variable "theme:bootstrap" --include-in-header "C:\Users\fabrn\AppData\Local\Temp\RtmpW6Vban\rmarkdown-str3490247b1f1e.html" --mathjax --variable "mathjax-url:https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"
Output created: new.html
processing file: new.spin.Rmd
|....................... | 33%
ordinary text without R code
|............................................... | 67%
label: unnamed-chunk-1
|......................................................................| 100%
ordinary text without R code
output file: new.knit.md
"C:/Users/fabrn/AppData/Local/Pandoc/pandoc" +RTS -K512m -RTS new.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output new.tex --lua-filter "C:\Users\fabrn\R\win-library\4.0\rmarkdown\rmarkdown\lua\pagebreak.lua" --lua-filter "C:\Users\fabrn\R\win-library\4.0\rmarkdown\rmarkdown\lua\latex-div.lua" --self-contained --highlight-style tango --pdf-engine pdflatex --variable graphics --variable "geometry:margin=1in"
This really is an issue when it comes to processing big data.
In real-world examples, I usually use latex output as a single rmarkdown::render output, then reprocess the .tex files using pandoc or similar tools (like prince for pdf). So my workflow is like:
rmarkdown::render('new.R', output_format = 'latex_document')
lapply(c("html", "pdf", ...),
function(form) rmarkdown::pandoc_convert("new.tex", output=paste0("new.", form)))
The bottom line is: all depends on your data. If small, output_format='all' is straightforward.
If big, you are better off with a common-ground format (latex is a good choice but html may be better in some cases) as an input to conversion tools.

sink() won't print output to text file in rmarkdown

Say I have a simple rmarkdown document called test.Rmd
---
output: pdf_document
---
This code tries to save output to a file called 'example.txt'
```{r}
sink(file='example.txt')
sink.number()
library(MASS)
summary(cars)
sink()
sink.number()
```
If I run this in RStudio (using the knit PDF button) then I get lots of output but I believe the most important is the following (I can include the other output on request)
processing file: test.Rmd
"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS test.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output test.pdf --template "C:\Users\wammonj\Documents\R\win-library\3.2\rmarkdown\rmd\latex\default.tex" --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in"
output file: test.knit.md
Output created: test.pdf
Warning message:
In sink() : no sink to remove
The file example.txt is made but the output is not in there while Rmarkdown made a file called test.pdf with the output of summary(cars) in it.
Is that the problem? Does Rmarkdown use sink() to make its documents? Is there a way around this so the output will appear in both the pdf file and the text file?
Addition: It looks like from #r2evans comment that rmarkdown does indeed use sink(). I have played with sink() a little and it seems like you can have multiple diversions going at the same time but you can only write to the one that was most recently activated (see example below).
So it seems from the output that Rmarkdown closes my sink down right away because when I look at sink.number() then it is always one.
I am still trying to find a workaround for this so any help would be nice.
Example of multiple diversions:
sink(file = 'example1.txt')
sink(file = 'example2.txt')
sink.number() # prints 2 to example2.txt
x = seq(1,10)
x # prints to example2.txt
sink()
sink.number() # prints 1 to example1.txt
y = sum(x)
y # prints to example1.txt
sink()
sink.number() # prints 0 to R console
I run into a similar issue. One solution of saving the output to local files is to use write.csv() function instead, which also works with non-csv files.
The R code below tries to save output to a file called 'example.txt'.
write.csv(data.frame(data_to_save), file='example.txt')
This issue is due to (I believe) an error in the evaluate package's use of sink(), combined with rmarkdown::render's line-by-line evaluation.
rmarkdown::render() will submit each line to evaluate::evaluate at a time, but evaluate will fail if a new sink() is created but not closed within that line. A workaround is to define these steps as a function in which both the initial sink(...) and the final sink() are submitted in one line. Of course all of the code you are attempting to capture the results of will need to be in the same function as well.
Alternately, wait to see if the rmarkdown or evaluate developers fix the issue:
https://github.com/r-lib/evaluate/issues/104
https://github.com/rstudio/rmarkdown/issues/2243
I wanted to do a parametric rendering of rmarkdown. The parameter would be a random number. I needed to save/see the output (a vector of number and string) to a file (save_out.txt).
str00 = c('test','some','here',2323)
cat(str00,file="save_out.txt",sep=",",append=TRUE)
cat('\n',file="save_out.txt",append=TRUE)
Result:
> test,some,here,2323
sink or writeLines do not work for me when I want to append data while rendering the rmarkdown.
I got some help from here: https://stackoverflow.com/a/55225065/1247080

What does “Knit HTML” do in Rstudio 0.98?

I am trying to figure out what command and default options RStudio uses when pressing the "knit HTML" button in RStudio version 0.98.1091 because I get a slightly different intermediate markdown file when I run the knit() function from the console.
Specifically, when I use the following header for the R markdown file:
---
title: "Report Title"
author: Daddy the Runner
date: "`r format(Sys.time(), '%A, %B %d, %Y')`"
output:
html_document:
keep_md: true
---
I get the following markdown file when pressing the "Knit HTML" button:
# Report Title
Daddy the Runner
`r format(Sys.time(), '%A, %B %d, %Y')`
When I execute the following command: knit("myReport.Rmd"), I get the following markdown file:
---
title: "Report Title"
author: Daddy the Runner
date: "Saturday, January 10, 2015"
output:
html_document:
keep_md: true
---
Clearly the RStudio button is generating the intermediate markdown file using some other options but I can't find any information about it in the RStudio docs.
The key issue is the date line. For some reason, RStudio doesn't execute the inline r chunk in the header when making the markdown file. (However, it does get executed before generating the final HTML.) Whereas, the knit() function call does execute the inline chunk while generating the markdown file.
The only other difference I noticed in the two markdown files is related to the generation of plots. The two methods generate different sized graphics (command line: 504 x 504) versus (button: 672 x 480) and place them in different directories.
I tried the recommendation in this What commands are run when pressing "Knit HTML" on an R Markdown file in Rstudio 0.96? question to insert a Sys.sleep(30) call but that did not provide any information about what call RStudio used to knit the document. It did pause the output in the R Markdown console window which was unnecessary because RStudio keeps all of the output anyway. What I didn't see in the output was the command RStudio issued.
Any insight to the nature of these differences would be greatly appreciated. While I like using IDE environments and the conveniences they provide, I really like to understand what it is they are doing so I can better anticipate their behavior.
As #rawr pointed out in the comments:
rmarkdown::render('your_document.Rmd', 'html_document', 'new_titel.html')
works and creates the same document as the Knit HTML button.
When I look at the RMarkdown tab (right of Console tab) it looks like they run knitr::knit and then a fairly involved pandoc shell line
/usr/local/lib/rstudio/bin/pandoc/pandoc filename.utf8.md --to html --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures --output filename.html --smart --email-obfuscation none --self-contained --standalone --section-divs --table-of-contents --toc-depth 3 --template /home/me/R/i686-pc-linux-gnu-library/3.1/rmarkdown/rmd/h/default.html --variable 'theme:flatly' --include-in-header /tmp/user/1001/RtmpKz5GnI/rmarkdown-str3bba3848bd7b.html --mathjax --variable 'mathjax-url:https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML' --no-highlight --variable highlightjs=/home/cd/R/i686-pc-linux-gnu-library/3.1/rmarkdown/rmd/h/highlight
From the very first /usr/local/lib/rstudio/bin/pandoc/pandoc I infer that they bring their own pandoc, probably figuring duplication is better than debugging to play nice with everyone's idiosyncratic pandoc versions.
So to me it looks like RStudio is doing the following:
knit
pandoc with their special pandoc version and a lot of flags
and step #2 is where the interpretation of your header
---
title: "Report Title"
author: Daddy the Runner
date: "`r format(Sys.time(), '%A, %B %d, %Y')`"
output:
html_document:
keep_md: true
---
happens.
HTH.
I believe it currently uses the html_document function in the RMarkdown package

Resources