Search code examples
rdplyrr-markdownknitrkableextra

KableExtra table works with knit button but does not work when rendered from a separate file


I know similar questions have been asked. However, I have tried all of those solutions as you will see in my code and none of them have worked. The strangest part is that my code has worked properly for months and only this week stopped working. The following code is within an RMD file and generates a table exactly as I want it when using the knit button on R Studio:

---
title: "Cars"
author: "xxx"
date: '`r format(Sys.Date(), "%B-%d-%Y")`'
always_allow_html: yes
output: pdf_document
classoption: portrait
header-includes:
  - \usepackage{booktabs}
  - \usepackage{longtable}
  - \usepackage{array}
  - \usepackage{multirow}
  - \usepackage{wrapfig}
  - \usepackage{float}
  - \usepackage{colortbl}
  - \usepackage{pdflscape}
  - \usepackage{tabu}
  - \usepackage{threeparttable}
  - \usepackage{threeparttablex}
  - \usepackage[normalem]{ulem}
  - \usepackage{makecell}
  - \usepackage{xcolor}
---

```{r}
library(kableExtra)
library(dplyr)

data <- cars

Portfolio <- c(4,7)

data%>%
  mutate(
    speed = cell_spec(speed, background= ifelse(speed %in% Portfolio, "yellow", "white"))
  ) %>%
  kable(escape = F, longtable= T) %>%
  kable_styling("striped", full_width = F, latex_options = "repeat_header")
```

The table looks like this:

Working table

It also continues into the next page.

I need to generate this report several times and so I have a separate R file where I loop the rendering of the previous code into separate PDF files.

# load packages
library(knitr)
library(rmarkdown)
library(kableExtra)
library(dplyr)


Portfolio <- c( 4,7)


# for each bond in the portfolio create a report
# these reports are saved in output_dir with the name specified by output_file
for (i in 1:length(Portfolio)){

  Bond <- Portfolio[i]

  rmarkdown::render("Table.Rmd" ,
                    output_format = "pdf_document",
                    output_file =  paste(Bond, ".pdf", sep=''),
                    output_dir = paste('Reports_',format(Sys.Date(), "%b-%d-%y")),
                    envir = new.env(
                      Bond <- Portfolio[i]
                    ))


} 

This code renders the PDF and everything in my reports look the same with the exception of kableExtra tables. Here is a picture of what the table looks like now:

Non Working Table

This is my first time asking a question on SO. I would appreciate any help for getting my code to work as well as advice on how to better format my questions in the future.


Solution

  • I think you need to specify the format (latex) for both the cell_spec and the kable calls. I don't really understand why but seems to work..

    data %>%
      mutate(
        speed = cell_spec(speed, format = "latex", background= ifelse(speed %in% Portfolio, "yellow", "white"))
      ) %>%
      kable(format = "latex", escape = F, longtable= T) %>%
      kable_styling("striped", full_width = F, latex_options = "repeat_header")