Parameterized Reports

R
Plotly
Reports
Time Series
Data Visualization
Achieving peak productivity in data analysis with automated R Markdown reports, minimizing effort and errors.
Author

Brian Cervantes Alvarez

Published

April 3, 2023

Modified

April 8, 2025

Yapper Labs | AI Summary Model: ChatGPT o3-mini-high

I automated the report generation process using parameterized R Markdown, which allowed me to update the year parameter dynamically and consistently produce reports without manual intervention. I developed custom themes and employed ggplotly to create interactive time series visualizations of monthly sales data, ensuring each report accurately reflected key trends and patterns. Additionally, I implemented a loop to render separate reports for multiple years, demonstrating efficiency and reproducibility in automated data reporting workflows.

Objective

This project was aimed at streamlining the report generation process by employing parameterized R Markdown. The core idea was to automate the update of report parameters, thus eliminating the manual task of adjusting values for daily or periodic reports. This innovation is designed to significantly reduce time and effort for data scientists, analysts, and engineers, facilitating the production of consistent and timely reports. The automation feature introduced through parameterized R Markdown enhances both productivity and accuracy, offering considerable benefits to business operations.

Approach

The process involved the following key steps:

  1. Library Utilization:
    • Essential R libraries were loaded, including tidyverse for data manipulation, plotly and ggplotly for interactive visualizations, and rmarkdown for rendering parameterized reports.
  2. Data Preparation:
    • The dataset was loaded and processed. This included renaming columns, converting dates, and filtering data based on the year specified through parameters.
  3. Custom Theme Development:
    • A custom theme function was created to standardize the appearance of plots across all reports, ensuring a consistent and professional aesthetic.
  4. Data Visualization:
    • Interactive visualizations were crafted using ggplotly to display monthly sales data, highlighting key trends and insights.
  5. Automated Report Generation:
    • A loop was implemented to automate the generation of reports for each year, utilizing a function that leverages parameterized R Markdown to render individual reports dynamically.

Highlights

  • The project underscores the efficiency and scalability of automated report generation.
  • The approach allows for the seamless creation of visually appealing and informative reports.
  • Automation minimizes errors and saves considerable time, enhancing decision-making processes with timely and accurate data insights.

Technical Notes

  • The script included the loading of required libraries, data wrangling steps, and the use of ggplotly for creating dynamic visualizations.
  • The custom theme function (myTheme) ensures a uniform look across all visualizations.
  • The renderReport function and subsequent loop for rendering reports underscore the automation aspect, showcasing the project’s capacity to produce multiple reports efficiently.

Conclusion

This project exemplifies the power of automation in report generation using parameterized R Markdown. It offers a scalable solution for producing detailed and aesthetically consistent reports, providing valuable time savings and accuracy for businesses and their data teams.

Required Libraries

Code
library(tidyverse)
library(plotly)
library(lubridate)
library(scales)
library(zoo)
library(rmarkdown)
library(purrr)

Load Dataset & Wrangle

Code
ds <- read_csv("../../../assets/datasets/retail.csv")

#head(ds)

ds <- ds %>% 
  rename(ID = ...1) %>%
  mutate(Month = lubridate::floor_date(Date, 'month')) %>%
  filter(year(Month) == params$year)

glimpse(ds)
Rows: 10,042
Columns: 9
$ ID         <dbl> 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1…
$ DocumentID <dbl> 716, 716, 716, 716, 716, 716, 716, 460, 461, 462, 463, 464,…
$ Date       <date> 2019-09-23, 2019-09-23, 2019-09-23, 2019-09-23, 2019-09-23…
$ SKU        <dbl> 1039, 853, 862, 868, 2313, 2355, 2529, 2361, 2723, 655, 254…
$ Price      <dbl> 381.78, 593.22, 423.73, 201.70, 345.76, 406.78, 542.38, 139…
$ Discount   <dbl> 67.37254, 0.00034, -0.00119, 35.58814, 61.01966, 101.69458,…
$ Customer   <dbl> 1, 1, 1, 1, 1, 1, 1, 460, 479, 26, 580, 311, 311, 311, 311,…
$ Quantity   <dbl> 1, 1, 1, 1, 1, 1, 1, 6, 1, 1, 1, 2, 2, 1, 1, 1, 4, 1, 1, 4,…
$ Month      <date> 2019-09-01, 2019-09-01, 2019-09-01, 2019-09-01, 2019-09-01…

Visualize The Report

I utilized ggplotly, a graphical representation tool, to create an interactive visualization of monthly sales time series data for “CRM and Invoicing system,” which is a wholesale company owned by Sadi Evren. The data for this analysis was obtained from the following Kaggle dataset: https://www.kaggle.com/datasets/shedai/retail-data-set?select=file_out.csv.

The resulting plot provided an insightful representation of the monthly sales data, showcasing trends and patterns in the data that could potentially provide useful information for decision making in the business.

In addition to the initial plot, I implemented a for loop to automatically generate multiple reports based on the time series data for each year. This approach eliminated the need for manual report generation, thereby saving time and reducing the risk of errors. The loop enabled the automated generation of separate reports for each year, which provided a comprehensive view of the sales trends over time.

Overall, the use of ggplotly for data visualization and automation of report generation using a for loop demonstrated an effective approach for efficiently analyzing and presenting data.

Code
p <- ds %>%
  group_by(Month) %>%
  summarize(AvgSales = round(mean(Price * Quantity),2) ) %>%
  ggplot(aes(x = Month, 
             y = AvgSales,
             group = 1,                 #Necessary or else line plot disappears
             text = paste0("Monthly Sales: $", (round(AvgSales/1000,2)),"K" ))) +
  geom_line(size = 1) + 
  scale_y_continuous(labels = scales::dollar_format(scale = .001, suffix = "K")) +
  scale_x_date(date_breaks = "1 month", date_labels = "%B") + 
  labs(title = paste0("CRM and Invoicing System Sales For FY: ", params$year),
       caption = "Source: https://www.kaggle.com/datasets/shedai/retail-data-set?select=file_out.csv",
       x = NULL,
       y = NULL) +
  myTheme()

ggplotly(p, tooltip = c("text")) %>% 
  layout(hovermode = "x unified") 

Function To Run Parameterized Reports

Code
renderReport <- function(year) {
  quarto::quarto_render(
    input = "index.qmd",
    output_file = paste0(year, '.html'),
    execute_params = list(year = year)
  )
}

Render All Reports

Code
# Renders all 4 Reports (dates range from 2019-2022)
for (year in 2019:2022) {
    renderReport(year)
}

Data References

Allaire, JJ, Yihui Xie, Christophe Dervieux, Jonathan McPherson, Javier Luraschi, Kevin Ushey, Aron Atkins, et al. 2024. Rmarkdown: Dynamic Documents for r. https://github.com/rstudio/rmarkdown.
Grolemund, Garrett, and Hadley Wickham. 2011. “Dates and Times Made Easy with lubridate.” Journal of Statistical Software 40 (3): 1–25. https://www.jstatsoft.org/v40/i03/.
Müller, Kirill, and Hadley Wickham. 2023. Tibble: Simple Data Frames. https://tibble.tidyverse.org/.
Sievert, Carson. 2020. Interactive Web-Based Data Visualization with r, Plotly, and Shiny. Chapman; Hall/CRC. https://plotly-r.com.
Sievert, Carson, Chris Parmer, Toby Hocking, Scott Chamberlain, Karthik Ram, Marianne Corvellec, and Pedro Despouy. 2024. Plotly: Create Interactive Web Graphics via Plotly.js. https://plotly-r.com.
Spinu, Vitalie, Garrett Grolemund, and Hadley Wickham. 2024. Lubridate: Make Dealing with Dates a Little Easier. https://lubridate.tidyverse.org.
Wickham, Hadley. 2016. Ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. https://ggplot2.tidyverse.org.
———. 2023a. Forcats: Tools for Working with Categorical Variables (Factors). https://forcats.tidyverse.org/.
———. 2023b. Stringr: Simple, Consistent Wrappers for Common String Operations. https://stringr.tidyverse.org.
———. 2023c. Tidyverse: Easily Install and Load the Tidyverse. https://tidyverse.tidyverse.org.
Wickham, Hadley, Mara Averick, Jennifer Bryan, Winston Chang, Lucy D’Agostino McGowan, Romain François, Garrett Grolemund, et al. 2019. “Welcome to the tidyverse.” Journal of Open Source Software 4 (43): 1686. https://doi.org/10.21105/joss.01686.
Wickham, Hadley, Winston Chang, Lionel Henry, Thomas Lin Pedersen, Kohske Takahashi, Claus Wilke, Kara Woo, Hiroaki Yutani, Dewey Dunnington, and Teun van den Brand. 2024. Ggplot2: Create Elegant Data Visualisations Using the Grammar of Graphics. https://ggplot2.tidyverse.org.
Wickham, Hadley, Romain François, Lionel Henry, Kirill Müller, and Davis Vaughan. 2023. Dplyr: A Grammar of Data Manipulation. https://dplyr.tidyverse.org.
Wickham, Hadley, and Lionel Henry. 2025. Purrr: Functional Programming Tools. https://purrr.tidyverse.org/.
Wickham, Hadley, Jim Hester, and Jennifer Bryan. 2024. Readr: Read Rectangular Text Data. https://readr.tidyverse.org.
Wickham, Hadley, Thomas Lin Pedersen, and Dana Seidel. 2023. Scales: Scale Functions for Visualization. https://scales.r-lib.org.
Wickham, Hadley, Davis Vaughan, and Maximilian Girlich. 2024. Tidyr: Tidy Messy Data. https://tidyr.tidyverse.org.
Xie, Yihui, J. J. Allaire, and Garrett Grolemund. 2018. R Markdown: The Definitive Guide. Boca Raton, Florida: Chapman; Hall/CRC. https://bookdown.org/yihui/rmarkdown.
Xie, Yihui, Christophe Dervieux, and Emily Riederer. 2020. R Markdown Cookbook. Boca Raton, Florida: Chapman; Hall/CRC. https://bookdown.org/yihui/rmarkdown-cookbook.
Zeileis, Achim, and Gabor Grothendieck. 2005. “Zoo: S3 Infrastructure for Regular and Irregular Time Series.” Journal of Statistical Software 14 (6): 1–27. https://doi.org/10.18637/jss.v014.i06.
Zeileis, Achim, Gabor Grothendieck, and Jeffrey A. Ryan. 2023. Zoo: S3 Infrastructure for Regular and Irregular Time Series (z’s Ordered Observations). https://zoo.R-Forge.R-project.org/.