0

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.

tim
  • 427
  • 1
  • 6
  • 10
  • When you are running the data.table code are you sure your data is a data.table and not a dataframe? What do you use to read the file? Do you explicitly convert the data to data.table? – Ronak Shah Feb 21 '21 at 07:11
  • In my actual Rmd I use fread() to read the data. In the example above the data.table(diamonds) step should convert the dataframe to a data.table. – tim Feb 21 '21 at 07:44
  • Do you perform any other data manipulation after importing the data? I have a doubt that your data is not a data.table hence you get the error. You can reproduce the error with`mtcars[, mpg := mpg * 2]` – Ronak Shah Feb 21 '21 at 08:52
  • I just edited my code to not rely on some pre-existing dataset. Just to be sure that I'm working with a datatable or dataframe. The error still exists. – tim Feb 21 '21 at 12:16
  • I copy your RMD for datatables and call them as `rmarkdown::render(input = "test.Rmd", output_file = "test.html", params = list(name = "datatable", factor = 5))`. It works for me without any error and generates a HTML file as output. I don't know what am I missing to reproduce this. – Ronak Shah Feb 22 '21 at 04:19
  • Yes. That’s what I said in the description. One file works fine. Using it in combination with multiple input files and mapping it with purrr::pwalk or just mapply causes the error. As it works when the data is in a data.frame format, I was guessing that the error is caused by the data.table format. – tim Feb 22 '21 at 11:23
  • This is old, but I had exact same error, looks like unresolved. Seems like something to do knitr conflicting with data.table's := in the chunk. I couldn't figure it out, so moved the data manipulation upstream before piping into params. – David Lucey Jan 07 '22 at 18:25

0 Answers0