Problems between kable and rmarkdown::render in a for loop

Viewed 364

I am trying to create multiple reports and knit them to a pdf in R. I have a script that generates the dataframe that I want to knit. Then, I have a for loop that splits the data corresponding to each student. This loops makes a call for a .Rmd notebook where I have specified the kable call to create the table. Here is a sample of my come

R script:

# Split files ####
student_IDs   <- unique(final_scores$ID)
nStudents     <- length(student_IDs)
student_names <- unique(final_scores$Name)

for (iStudent in 1:nStudents) {

  scores <- filter(final_scores, ID == student_IDs[iStudent]) %>% 
    select(-c(ID, Name))


  rmarkdown::render(envir = new.env(),
                    input         = "grades_handout.Rmd",
                    output_format = "pdf_document",
                    output_file   = paste(student_names[iStudent], student_IDs[iStudent], "assignsubmission_file", student_names[iStudent], "HW3_Grade_",
                                          sep = "_"),
                    output_dir    = file.path(root_dir, "Rubrics"))
}

Rmd:

---
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}
output: pdf_document
classoption: landscape
geometry: margin=2cm
---
\pagenumbering{gobble}


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

```

```{r echo=F, warning=FALSE}
kable(grade, format = "latex", 
      booktabs = T, 
      align = "c") %>%
  column_spec(1:4, width = "6em") %>%
  kable_styling(font_size = 12) %>%
  row_spec(0, align = "c", bold = T)

```

```{r echo=F, warning=F}
kable(scores, format = "latex",
      longtable = T, #linesep = "",
      booktabs = T,
      col.names = c("Q.", "Level I", "Level II", "Level III", "Value",
                    "Correct Answer", "Your Answer",
                    "Points answer", "Points work", "Feedback"),
      align = c("c", "c", "c", "c", "c", "l", "l", "c", "c", "l")) %>%
  kable_styling(latex_options = c("repeat_header", "striped"), full_width = F) %>%
  column_spec(6:7, width = "15em") %>%
  column_spec(1:4, width = "1.5em") %>%
  column_spec(5, width = "1.5em") %>%
  column_spec(8:9, width = "3em") %>%
  column_spec(10, width = "15em") %>%
  row_spec(0, align = "c")


```

If I click on "knit", the process works flawlessly and the document is produced. However, when calling from the script using render(), some of the options selected in kable cause crashes during the rendering. Using the option "striped", inside the call for kable_styling(), causes the following output:

! Misplaced \noalign.
\rowcolor ->\noalign 
                     {\ifnum 0=`}\fi \global \let \CT@do@color \CT@@do@color...
l.113 \rowcolor
               {gray!6}  Incorrect coefficient for beta(b).\\ 

I understand this is probably due the column width making some cells to be multiple rows, but correct me if I am wrong. The point is, I don't understand what is the difference when clicking the button "Knit" vs using rmarkdown::render().

If any of you could point me in the direction of how to include the missing information in the YAML header (if that is necessary, I had to do that with the packages that kable uses, otherwise other errors appeared) or how to give more information to render() so it can run properly.

Best, Eduardo

0 Answers
Related