4

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.

TTS
  • 1,818
  • 7
  • 16
  • 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. Jun 24 '20 at 21:02
  • Pictures loaded. Also adding a new behavior description. – TTS Jun 25 '20 at 14:14
  • My table output goes wacky too just like your, when I do \usepackage{fancyhdr}. It wasn't always like this though. – Diego Sep 12 '22 at 03:10
  • I just ran it a second time and it worked. so unconsistent. – Diego Sep 12 '22 at 03:15

1 Answers1

2

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
---
fishdata
  • 113
  • 6
  • Further Explanation: It appears that the added LaTeX packages need to be present for the `render` function to recognize and include them, while the `knit` button apparently recognizes the need for those packages even if they aren't included in the YAML. – TTS Jul 28 '20 at 15:31
  • 1
    The clue was given in the error message `! LaTeX Error: Environment landscape undefined` and by adding `keep_tex: true` to the YAML. If you look at the `.tex` file it should now include the the packages `pdflscape` and `booktabs` when it didn't before. – fishdata Jul 28 '20 at 15:41