In RStudio, `knit` always works, but `rmarkdown::render` fails on second run (but not first!)
Asked Answered
M

3

8

I'm trying to do something quite simple: generate reports in PDF format. Finally found a way that reproduces my issue. I need to use rmarkdown::render to create reports based on data in GlobalEnv. I am using the tinytex package. Here is test.Rmd:

---
title: "Untitled"
output: pdf_document
---

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

## R Markdown

```{r cars}
mtcars %>%
  kable(booktabs = TRUE) %>%
  kable_styling(latex_options = "striped")
```

Works:

"Knit" in RStudio seems to always work on this file, producing, as expected, the mtcars dataframe, nicely formatted with kable()

Doesn't work (but should?):

Running rmarkdown::render("test.Rmd") works on THE FIRST RUN, but NOT the second. It throws the error:

! LaTeX Error: Unknown float option `H'.

After this, "Knit" in RStudio produces the PDF, but R/knitr prints any warning/error messages from the rmarkdown::render("test.Rmd") command.

Additional Information

Running rmarkdown::render("test.Rmd") does not produce errors if the above code chunk is changed to

```{r cars}
mtcars %>%
  kable()
```
Mackay answered 15/6, 2019 at 0:0 Comment(0)
C
6

That didn't work for me. I was getting a clash in xcolor option. The solution as pointed out in page 3 : https://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf

is to add :

options(kableExtra.latex.load_packages = FALSE)

and in the YAML :

header-includes:
- \usepackage{booktabs}
- \usepackage{longtable}
- \usepackage{array}
- \usepackage{multirow}
- \usepackage{xcolor}
- \usepackage{wrapfig}
- \usepackage{float}
- \usepackage{colortbl}
- \usepackage{pdflscape}
- \usepackage{tabu}
- \usepackage{threeparttable}
- \usepackage{threeparttablex}
- \usepackage[normalem]{ulem}
- \usepackage{makecell}

Note that you can remove any packages that you don't need.

Cupid answered 18/3, 2021 at 18:53 Comment(0)
M
3

I've elected to answer my own question because I've found a work-around that hopefully will not be necessary if someone finds the reason for the errors.

It seems that the PDF rendering engine does not recognize anything but the most basic LaTeX installation of tinytex. I tried tinytex::tlmgr_install to manually install the necessary LaTeX packages but all of them returned a "package already present" message.

Solution

I've added the following to my YAML in my Rmd:

header-includes:
   - \usepackage{booktabs}
   - \usepackage{float}
   - \usepackage{colortbl}
   - \usepackage[table]{xcolor}

I essentially added each \usepackage line until I received no errors with the formatting I was looking for.

Working Rmd code

Both rmarkdown::render() and knit (Rstudio) work (and on my OWN code as well!):

---
title: "Untitled"
output: pdf_document
header-includes:
   - \usepackage{booktabs}
   - \usepackage{float}
   - \usepackage{colortbl}
   - \usepackage[table]{xcolor}
---

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

## R Markdown

```{r cars}
mtcars %>%
  kable(booktabs = TRUE) %>%
  kable_styling(latex_options = "striped")
```
Mackay answered 17/6, 2019 at 21:41 Comment(0)
C
2

Using Rscript_call solved the problem for me in such cases.

library(rmarkdown)

# doesn't work on second run
render(input = "K:/file.Rmd")

# works on second run
xfun::Rscript_call(
  rmarkdown::render,
  list(input = 'K:/file.Rmd')
)
Colfin answered 12/4, 2022 at 10:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.