4
votes

I have some Rmarkdown documents I am trying to knit/renderas PDF files. I want to get render to work properly because I am going to run this in a script with multiple Rmd files as well as a lot of other processes (e.g. data grabs & processing).

Using the knit button produces the desired result. If I use rmarkdown::render the table layout goes wacky (see example). Here's a minimal example that has reproduced my issue.

RMD

---
title: "RmdTest"
author: "TTS"
date: '`r format(Sys.time(), "%d %B, %Y")`'
output: pdf_document
always_allow_html: true
---

```{r Rmd-Setup, include=FALSE}
options(knitr.kable.NA = '')

library(kableExtra)

# Dummy Data
df <- structure(list(Location = c("Farm", "Farm", "Farm", "Farm"), 
    Animal = c("dog", "cat", "cat", "cat"), Age = c("Adult", 
    "Juvenile", "Adult", "Total"), Abundance = c(27269L, 62308L, 
    34904L, 97212L)), row.names = c(NA, -4L), class = "data.frame")
```


## Why?

The 'Knit' button is producing the desired result, while running rmarkdown::render is producing a different (undesirable) result.

```{r Table-1, echo=FALSE}
kable(df, caption = 'This data does not make sense.', booktabs = TRUE) %>%
  kable_styling(latex_options = 'scale_down') %>%
  landscape() %>%
  add_footnote(label = 'Here is a footnote.') 
```

Render

  rmarkdown::render(input = 'test.Rmd', output_format = "pdf_document")

Versions

R version 4.0.0 (2020-04-24)
Rmarkdown v 1.1
kableExtra v 1.1.0

Desired Outcome: To get render to have the same output as the knit button, specifically the table formatting. Any help is appreciated. Please let me know if any other info would help. Cheers!

Screenshots

Using Knit

Using Render

New Strange Behavior

After restarting my R session by exiting and restarting RStudio, I am able to run the render successfully, with the desired formatting. Attempting to run render again immediately after is returning the error message: ! LaTeX Error: Environment landscape undefined.

However, if I use .rs.restartR(), render produces the incorrect formatting. Running the render afterwards produces the same result: produces a PDF with the incorrect formatting.

Note: I reinstalled tinytex this morning to make sure that wasn't the issue.

1
I cannot reproduce this: When I run your code, the knit button and rmarkdown::render produce exactly the same result. Can you post screenshots of the issue? - CL.
Pictures loaded. Also adding a new behavior description. - TTS

1 Answers

2
votes

Try including the correct packages for Latex in the YAML preamble. I'm not sure why selecting the Knit button in the Rstudio UI and using the render function work differently; but they do. I have found including the Latex packages usually fixes the problem.

---
title: "RmdTest"
author: "TTS"
date: '`r format(Sys.time(), "%d %B, %Y")`'
header-includes:
- \usepackage{pdflscape}
- \usepackage{booktabs}
output: pdf_document
always_allow_html: true
---