kableExtra prevents knit to PDF

I have been trying to work with RMarkdown more and use it for communication with my team (who are not R users), and to that end, I've been trying to do reports out to PDF. I've been creating tables with kable(), but found that additional formatting is necessary, so I've been trying to work with kableExtra, but find that it hangs on the rendering of the PDF.

Below is my simplified RMarkdown file. Although I want to use kable_styling(), I have it commented out here, and what is bothering me is that the PDF will or won't render solely based on whether library(kableExtra) is included or not. I'm not even using the kableExtra functions -- just calling the library causes failure to render.
(Note: Below I have removed the closing "```" from the code chunks in order to show the file as close as possible.)

---
title: "Troubleshooting RMarkdown to PDF"
author: "Scott Jackson <br/> Frustrated RMarkdown Learner"
date: "`r format(Sys.Date(), '%B %d, %Y')`"
output: pdf_document
editor_options:
  chunk_output_type: console
---

```{r setup, include=FALSE}
library(knitr)
library(kableExtra)   # If you comment out this line, the PDF will render; as-is though, it never completes the render.
library(tidyverse)

knitr::opts_chunk$set(echo = FALSE)
options(knitr.table.format = "latex")

## A Simple Test
Running this next chuck works when run interactively, but not when knit to a PDF.

```{r test}
mtcars[1:5, 1:4] %>% 
  kable(col.names =c("MPG", "# Cyl", "Displacement", "Horsepower"),
        caption = "mtcars example")  #%>% 
  # kable_styling(font_size = 12)
> sessionInfo()
R version 4.3.2 (2023-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)

Matrix products: default


locale:
[1] LC_COLLATE=English_United States.utf8  LC_CTYPE=C                             LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C                           LC_TIME=English_United States.utf8    

time zone: America/New_York
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] kableExtra_1.3.4.9000 MondelezR_0.1.0.9001  janitor_2.2.0         lubridate_1.9.3       forcats_1.0.0        
 [6] stringr_1.5.0         dplyr_1.1.3           purrr_1.0.2           readr_2.1.4           tidyr_1.3.0          
[11] tibble_3.2.1          ggplot2_3.4.4         tidyverse_2.0.0       knitr_1.45           

loaded via a namespace (and not attached):
 [1] sass_0.4.7        utf8_1.2.4        generics_0.1.3    xml2_1.3.5        stringi_1.7.12    hms_1.1.3         digest_0.6.33    
 [8] magrittr_2.0.3    evaluate_0.23     grid_4.3.2        timechange_0.2.0  fastmap_1.1.1     jsonlite_1.8.7    httr_1.4.7       
[15] rvest_1.0.3       fansi_1.0.5       viridisLite_0.4.2 scales_1.2.1      jquerylib_0.1.4   cli_3.6.1         rlang_1.1.2      
[22] munsell_0.5.0     cachem_1.0.8      withr_2.5.2       yaml_2.3.7        tools_4.3.2       tzdb_0.4.0        colorspace_2.1-0 
[29] webshot_0.5.5     vctrs_0.6.4       R6_2.5.1          lifecycle_1.0.4   snakecase_0.11.1  pkgconfig_2.0.3   bslib_0.5.1      
[36] pillar_1.9.0      gtable_0.3.4      glue_1.6.2        systemfonts_1.0.5 highr_0.10        xfun_0.41         tidyselect_1.2.0 
[43] rstudioapi_0.15.0 htmltools_0.5.7   rmarkdown_2.25    svglite_2.1.2     compiler_4.3.2   

Why does this happen? I have this test file stripped to the simplest version of itself.

It is late here and I am just about to go to bed.

This seems to run but I'm not sure what change I made worked. Possibly a missing ```

--
title: "Troubleshooting RMarkdown to PDF"
author: "Scott Jackson <br/> Frustrated RMarkdown Learner"
date: "`r format(Sys.Date(), '%B %d, %Y')`"
output: pdf_document
---

```{r setup, include=FALSE}
library(knitr)
library(kableExtra)
library(tidyverse)

knitr::opts_chunk$set(echo = FALSE)
options(knitr.table.format = "latex")

A Simple Test

Running this next chuck works when run interactively, but not when knit to a PDF.

mtcars[1:5, 1:4] %>% 
  kable(col.names =c("MPG", "# Cyl", "Displacement", "Horsepower"),
        caption = "mtcars example")

Note that you are not running in RStudio, although I don't know why that should matter.

You're going to hate this...ran fine for me in RStudio.

Thank you. I had trouble putting the ``` into the OP since I'm trying to write RMarkdown inside of regular Markdown (worlds colliding, etc.). On my computer, I was using backticks ``` (Oh, I just realized that maybe I can just escape them with a backslash???) to properly delineate the code chunks, but not sure how to show that in these posts.

I can run the code interactively, too. It's when I knit to PDF it fails when library(kableExtra) is called (but not used.

I'm not running RStudio? I don't understand.

You're successfully getting that to knit to PDF?

Thank you for looking at this.

Sorry. I misread your picture as being the R GUI. Of course, you're running RStudio,

Yes, it ran for me. What error message do you get when you knit?

There's no error message. I just get that "Render" tab with a very small "churning" circle like it is working, but it never ends. It's the screenshot I posted at the end. It just renders forever without end.

And, if I just comment out library(kableExtra) from the file (which isn't being used since I have already commented out the kable_styling() function that comes from that package -- the file will knit just fine to PDF. So I'm just perplexed why the inclusion of kableExtra prevents the knitting.

I just ran it again, inserting the backticks as @jrkrideau suggested. It ran fine and nearly instantly.

That's not very helpful, is it....

Maybe re-install the kableExtra package?

I can't reproduce. This doc renders well for me

---
title: "Troubleshooting RMarkdown to PDF"
author: "Scott Jackson <br/> Frustrated RMarkdown Learner"
date: "`r format(Sys.Date(), '%B %d, %Y')`"
output: 
  pdf_document:
    keep_md: true
editor_options:
  chunk_output_type: console
---

```{r setup, include=FALSE}
library(knitr)
library(kableExtra)   # If you comment out this line, the PDF will render; as-is though, it never completes the render.
library(tidyverse)

knitr::opts_chunk$set(echo = FALSE)
options(knitr.table.format = "latex")
```

## A Simple Test
Running this next chuck works when run interactively, but not when knit to a PDF.

```{r test}
mtcars[1:5, 1:4] %>% 
  kable(col.names =c("MPG", "# Cyl", "Displacement", "Horsepower"),
        caption = "mtcars example")  %>% 
  kable_styling(font_size = 12)
```

kableExtra will modify knitr::kable() default as soon as you load it. This is explained in their doc:

Starting fromkableExtra0.9.0,whenyouloadthispackage(library(kableExtra)), itwillautomaticallysetup theglobaloption‘knitr.table.format’basedonyourcurrentenvironment.UnlessyouarerenderingaPDF, kableExtrawill trytorenderaHTMLtableforyou.

This means that latex format will use when kableExtra is loaded, otherwise, it could output Markdown by default.

You can set the keep_md: true under pdf_document to see the different in the intermediate output.

Though, you are setting the option yourself above, so not sure this is linked

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.