Why Does Rendering a PDF from Rmarkdown Require Closing Rstudio Between Renders

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

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().

Why does RMarkdown `render` behavior depend on whether it's called from RStudio Server or from a PHP shell?

Usually, issues of this form (where unicode characters are converted to a unicode code point representation, e.g. <U+00EB> in this case) are caused by an attempt to run R within a non-UTF-8 locale.

Typically, this can be verified by checking the output of Sys.getlocale("LC_ALL"). If you see a C locale reported, then you likely need to enforce a UTF-8 locale with something like:

Sys.setlocale("LC_ALL", "en_US.UTF-8")

substituting the particular UTF-8 locale flavor based on your desired language. (For reference, the set of available locales can usually be queried from a terminal with something like locale -a).

Render multiple outputs from one Rmd

Option 1

you can use rmarkdown::render in knit yaml key and specify ouput_format = "all" to render the documents to all the specified formats in the yaml section.

---
title: Render a table in a tiny environment
output:
pdf_document: default
html_document: default
knit: (function(inputFile, encoding) {
rmarkdown::render(inputFile, encoding = encoding,
output_dir = "output", output_format = "all") })
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting
syntax for authoring HTML, PDF, and MS Word documents.
For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

After clicking on Knit, you will have both pdf and html file in the output folder.

Option 2

Which is actually the same thing as option 1, but uses a bit different workflow.

In your Rmd file yaml section, you simply specify the output format like this,

---
title: Render a table in a tiny environment
output:
pdf_document: default
html_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting
syntax for authoring HTML, PDF, and MS Word documents.
For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

Then in the console or another r-script, run this,

rmarkdown::render('multiple_output.Rmd',output_dir = "output", output_format = 'all')

Again, similarly you will have both pdf and html file in the output folder.



Related Topics



Leave a reply



Submit