R markdown PDF renders in Rstudio but not in R console - r

Update: After hours worth of trying stuff, I figured it out from this thread. Basically, I just had to detach the package KableExtra in between each call to render(), and all of the PDFs rendered in RConsole.
I have a series of R markdown documents that render in RStudio by clicking on the "Knit" button. I'm trying to write a script that will render all of the documents at once instead of waiting to click the button after each one renders, but when I run it in the Console, I keep getting errors.
Calls to render are as follows:
render(input="path/markdown_file.Rmd",output_format="pdf_document")
Here's what I know:
Both RStudio and R Console are running R version 4.2.2
Both are using pandoc version 2.19.2
Both have current versions of the rmarkdown package
Both are using pdflatex to render the PDFs
Both are rendering HTML files using html_document with no issues
The error message I'm getting in RConsole is:
`! Undefined control sequence.
...r}[t]{llrrrrrrrrrrrrrrrrl} \toprule \textbf {metric} & \textbf... l.693 \end{tabular}}
Error: LaTeX failed to compile markdown_file.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See markdown_file.log for more info.`
The only difference I'm seeing is a slight difference in the calls to pandoc:
`"C:/Program Files/RStudio/bin/quarto/bin/tools/pandoc" +RTS -K512m -RTS markdown_file.knit.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output markdown_file.tex --lua-filter "C:\Users\XXXXXXX\AppData\Local\R\win-library\4.2\rmarkdown\rmarkdown\lua\pagebreak.lua" --lua-filter "C:\Users\XXXXXXX\AppData\Local\R\win-library\4.2\rmarkdown\rmarkdown\lua\latex-div.lua" --embed-resources --standalone --highlight-style tango --pdf-engine pdflatex --variable graphics *--include-in-header "C:\Users\XXXXXXX\AppData\Local\Temp\RtmpoJICVK\rmarkdown-str196079161973.html"*`
The Knit button in RStudio has the last part in italics, but the R Console does not. It's some reference to a temp file, but it gets deleted immediately so I can't see what's in it.
Any ideas on how to fix this?

Related

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)

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

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}

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.

Howto include js dependencies of DT datatable in Rmarkdown using knitr and pandoc

Is there a way to compile RMarkdown documents with datatables produced by DT outside of RStudio?
I'm trying to include a datatable widget in an RMarkdown document that I then want to convert to html using knitr and pandoc. This works fine with RStudio but if I try to do the same with knitr and pandoc I'm unable to get a working html file.
Here is a minimal example of an Rmd file that works with RStudio but not otherwise:
---
title: "Minimal DT example"
---
<style type="text/css"> table, table th, table td { border: none; } </style>
```{r}
library(DT)
datatable(iris)
```
Which I would then like to convert to html using:
knitr::knit('example.Rmd')
knitr::pandoc('example.md',format="html")
I am aware that RStudio uses a much more complicated pandoc call:
/usr/lib/rstudio/bin/pandoc/pandoc scratch.utf8.md --to html --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures --output scratch.html --smart --email-obfuscation none --self-contained --standalone --section-divs --template /home/user/R/x86_64-pc-linux-gnu-library/3.1/rmarkdown/rmd/h/default.html --variable 'theme:bootstrap' --include-in-header /tmp/RtmpMLtVfF/rmarkdown-str24935297671d.html --mathjax --variable 'mathjax-url:https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML' --no-highlight --variable highlightjs=/home/user/R/x86_64-pc-linux-gnu-library/3.1/rmarkdown/rmd/h/highlight
which I can recreate up to the part where this tmp file is included in the header:
/tmp/RtmpMLtVfF/rmarkdown-str24935297671d.html
I assume that this file contains the js sources for jquery and datatables. I have tried to add them by hand from the package source - but not succeeded - and would anyways like a solution that works out of the box.
The package RStudio is using under the hood is rmarkdown. It handles the Knit command and delegates work to knitr and pandoc, and it's also responsible for bundling dependencies and injecting them into the doc.
You likely already have the package installed, so you should be able to do this to produce your HTML file in one go:
rmarkdown::render('example.Rmd')
More resources:
RMarkdown on CRAN
RMarkdown Introduction

rstudio knitr Error: pandoc document conversion failed with error 43

Windows7 Prof 64-bit
Rstudio Version 0.98.1091
Hello all,
I am receiving the following error when I try to use KnitR to create a PDF.
Error: pandoc document conversion failed with error 43
In addition: Warning message:
running command '"C:/Program Files/RStudio/bin/pandoc/pandoc" FirmsoftReportGenerator.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures --output
{....}
\Documents\R\win-library\3.1\rmarkdown\rmd\latex\default.tex" --highlight-style tango --latex-engine pdflatex --variable "geometry:margin=1in"' had status 43
I have searched the forum and I found 2 references to this, and that led me to update my Rmarkdown package. However, I still receive the error.
My code consists of sourcing an R file, creating a table.
Then doing the following:
```{r outputSection, results="asis", echo=FALSE}
library(xtable)
print(xtable(overallSummary), comment=FALSE)
```
Total Raw Messages are: `r totalRawMessages`.
Any help in the right direction would be huge!
Thanks in advance
PS - The script does produce a PDF using a different CSV as a source.
The primary difference I notice with the newer source file is that one of the rownames has a space in the table I am trying to print.
PPS - When I manually replace the Row Name with spaces with "TEST1", everything works fine
rownames(overallSummary)[1] <- "TEST1"

Resources