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"
Related
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?
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)
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}
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.
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