Search code examples
rr-markdownknitrrender

Rmarkdown render within a loop - why aren't my parameters being passed?


I am trying to compile multiple documents via Rmarkdown.

I am using the "params" argument, and I am declaring these in yaml header of the Rmarkdown document. But I am still getting the error

Error in knit_params_get(input_lines, params) : 
render params not declared in YAML: student_no

Below is the code I am using to do this (R)


library(rmarkdown)
library(tidyverse)

setwd("/path/to/directory")

df = read_csv("2324_data_for_student_reports.csv")

ids = unique(df$CandidateId)

for (i in 1:length(ids)){
  rmarkdown::render(
    input = "2324 student reports - generate graphs.Rmd",
    params = list(student_no=ids[i]),
    output_format = "word_document",
    output_file = paste0(as.character(ids[i]), ".docx")
  )
  
}

and here Rmarkdown code to render the graph


    ---
    title: "Student report"
    params:
      student_no:
    ---
    
    
    
    ```{r setup}
    setwd("/path/do/directory")
    library(tidyverse)
    library(psych)
    theme_set(theme_bw())
    ```
    
    
    ```{r}
    
    df <- read_csv("2324_data_for_student_reports.csv")
    
    
    ```
    
    # Report for Student Number `r params$student_no`
    
    ```{r}
    
    
    df.means = df %>% group_by(topic) %>% summarise(score = mean(score))
    df.id = df %>% filter(CandidateId == params$student_no) %>% group_by(topic) %>% summarise(score = mean(score))
    
    g = ggplot(data = df.means,
               aes(x = topic, y = score))
    g = g + geom_bar(stat = "identity", fill = "grey70")
    g = g + scale_x_discrete(guide = guide_axis(angle = 50))
    g = g + ylim(0, 1)
    g = g + geom_point(data = df.id, aes(x = topic, y = score),
                       shape = 15,
                       size = 3)
    g


Solution

  • You need to provide a default value to parametesr when you declare them. See https://bookdown.org/yihui/rmarkdown/params-declare.html

    Try updating your Rmd header to

    ---
    title: "Student report"
    params:
      student_no: 1
    ---