tim
tim

Reputation: 437

Rendering multiple Rmarkdown reports with parameters fails with "Error: `:=` can only be used within a quasiquoted argument" when data.table is used

Good morning everybody,

as stated above, I’m trying to render multiple Rmarkdown reports with different parameters for each report. Basically I have a folder of .csv files, which I have to clean up. I have packed all the steps in an .Rmd file, because this way the data gets cleaned and a short report is generated documenting the results. Some figures, some stats, nothing very dramatic, just an overview of how the cleaning went.

As each .csv file is slightly different, I have to tweak some parameters. This is the easy part. I have found some nice code in the “R for Data Science” book, which you can find here. https://r4ds.had.co.nz/r-markdown.html#parameters

This is my version:

library(dplyr)
library(stringr)
library(purrr)

# Create a vector with names
files <-  c("dataframe", "datatable")


# Create a tibble with filenames and lists of parameters
reports <- tibble(
  filename = str_c(files, ".html"),
  params = map(files, ~ list(name = .,
                             factor = if_else(. == "dataframe", 2.5, 5))))

  

#-------------------------------------------------------------------

# make reports
reports <- reports %>%
  select(output_file = filename, params) %>% 
  purrr::pwalk(rmarkdown::render, input = "template_datatable.Rmd")


Everything runs fine, when the .Rmd file uses data.frames. As my .csv are about 1 GB each, I would use data.table to speed things up. But as soon as my .Rmd file contains some data.table code I get this error message:

Error: `:=` can only be used within a quasiquoted argument

If I just render one file with rmarkdown::render(input = "template_datatable.Rmd", output_file = "test.html", params = list(name = "datatable", carat = 5)), the .Rmd with the data.table code works fine.

My questions are. What is causing this error? And is there a way to fix it?

Here is my code for the .Rmd using data.frames:

    ---
    title: "A report for `r params$name`"
    params:
      name: "name"
      factor: 1
    output:
        bookdown::html_document2:
        fig_caption: yes
        toc: yes
        toc_float: true
        code_folding: "hide"
    ---
    
    ```{r setup, include=FALSE}
    
    # Setup Chunk
    
    # Some knitr options
    knitr::opts_chunk$set(echo = FALSE)
    
    # Packages
    library(dplyr)
    library(ggplot2)
    
    ```
    
    
    ```{r dataImport}
    
    df <- data.frame(A = seq(1, 100), B = seq(1, 100))
    
    df <- df %>%
      mutate(C = B * params$factor)
    
    ```
    
    
    ```{r makePlot}
    
    ggplot(df, aes(A, C)) + 
      geom_line()
    
    ```

And my code for the .Rmd using data.tables:

    ```
    ---
    title: "A report for `r params$name`"
    params:
      name: "name"
      factor: 1
    output:
      bookdown::html_document2:
        fig_caption: yes
        toc: yes
        toc_float: true
        code_folding: "hide"
    ---
    
    ```{r setup, include=FALSE}
    
    # Setup Chunk
    
    # Some knitr options
    knitr::opts_chunk$set(echo = FALSE)
    
    # Packages
    library(data.table)
    library(ggplot2)
    
    ```
    
    
    ```{r dataImport}
    
    dt <- data.table(A = seq(1, 100), B = seq(1, 100))
    
    dt <- dt[, C := B*params$factor]
    
    ```
    
    
    ```{r makePlot}
    
    ggplot(dt, aes(A, C)) + 
      geom_line()
    
    ```

Thanks for your help.

Upvotes: 0

Views: 520

Answers (0)

Related Questions