Comprehensive Reporting
Combining Documents and Dashboards
Effective reporting goes beyond presenting static results—it allows stakeholders to explore the data, interact with key metrics, and understand insights in real-time. Quarto offers a powerful framework to combine static documents (PDF, HTML, Word) with dynamic, interactive dashboards. In this post, we will walk you through:
- Creating multi-output reports (PDF, HTML, Word)
- Incorporating interactive dashboards into reports
By the end of this post, you’ll know how to generate comprehensive, multi-format reports with embedded interactive elements.
1. Creating Multi-Output Reports (PDF, HTML, Word)
Quarto supports the creation of reports in multiple formats from a single source file. This flexibility allows you to cater to different stakeholders who may need reports in different formats (e.g., for printing, sharing online, or editing in Word).
Step 1: Define Output Formats in the YAML Header
To create a report that outputs in multiple formats, you simply need to specify the desired formats in the YAML header. Here’s an example:
yaml
Copy code
---
title: "Sales Performance Report"
author: "Data Analyst"
output:
html_document:
toc: true
pdf_document:
toc: true
latex_engine: xelatex
word_document:
toc: true
reference_docx: "custom-template.docx"
---
In this example:
- HTML format: Generates a report with a table of contents (
toc
). - PDF format: Uses LaTeX for PDF generation, also with a table of contents.
- Word format: Outputs a Word document, with a custom template for styling.
Step 2: Render the Document for Multiple Formats
To generate reports in all formats, run the following command:
quarto render report.qmd
This will create the report in the specified formats (HTML, PDF, and Word), all from the same source .qmd
file.
Customizing Output Styles
For PDF and Word formats, you can further customize the output:
- PDF: Customize the layout and design using LaTeX. This can involve setting margins, headers, footers, font styles, and more.
- Word: Create a custom Word template (
custom-template.docx
) to define styles like headers, footers, table styles, etc.
For example, in the YAML header, you can specify a custom LaTeX template for PDF output:
pdf_document:
template: "custom-template.tex"
Managing Output for Different Audiences
- HTML: Ideal for sharing online or presenting dynamic content.
- PDF: Best for printing or archiving.
- Word: Perfect for editable reports that others can collaborate on or further edit.
By defining the right output formats, you can ensure that your report is optimized for different purposes, all from a single source.
2. Incorporating Dashboards into Reports
Interactive dashboards allow users to explore data, filter, and visualize insights in real time. Quarto allows you to embed interactive dashboards in your reports, making them much more engaging and informative.
Step 1: Using Shiny to Add Interactivity
Quarto integrates smoothly with Shiny, the R package for building interactive web applications. Here’s how you can incorporate a Shiny dashboard into a Quarto report.
Creating a Simple Shiny Dashboard
Let’s create a dashboard that displays a plot with a dynamic slider. Add this code inside your .qmd
file:
library(shiny)
library(ggplot2)
<- fluidPage(
ui titlePanel("Sales Dashboard"),
sidebarLayout(
sidebarPanel(
sliderInput("year", "Select Year", min = 2018, max = 2024, value = 2024)
),mainPanel(
plotOutput("salesPlot")
)
)
)
<- function(input, output) {
server $salesPlot <- renderPlot({
output<- mtcars[mtcars$mpg == input$year, ]
data ggplot(data, aes(x = wt, y = mpg)) + geom_point()
})
}
shinyApp(ui = ui, server = server)
In this Shiny app:
- The
sliderInput
widget allows users to select a year. - The
renderPlot
function dynamically generates a plot based on the selected year.
Step 2: Rendering the Quarto Report
Once your Shiny app is embedded in the .qmd
file, render the document using:
quarto render report.qmd
This will produce an interactive HTML document with the Shiny dashboard embedded.
Step 3: Combining Static Content with Dashboards
You can mix static content (e.g., text, tables, static charts) with interactive elements like dashboards within the same report. For example, before the Shiny app in the document, you can add a summary section:
## Sales Performance Summary
This section provides an overview of sales performance, highlighting the key metrics for the selected year. Use the interactive dashboard below to explore the sales data dynamically.
This allows you to provide contextual information before letting the user interact with the data.
Step 4: Customize Shiny Dashboards for Reports
You can also customize the layout and features of your Shiny app to make it more suitable for a report. Consider adding:
- Input Widgets: Dropdowns, sliders, checkboxes to allow user interactions.
- Dynamic Tables: Interactive tables with filters and sorting capabilities (using
DT
orreactable
). - Tabs or Panels: Organize different parts of the dashboard into tabbed sections for easier navigation.
Step 5: Publishing Dashboards
Once the report with the embedded Shiny dashboard is ready, you can publish it:
- HTML: Can be published as a static report with embedded Shiny interactivity.
- RStudio Connect: For more advanced deployment, you can host the interactive report on RStudio Connect, which supports Shiny and Quarto integration.
Best Practices for Combining Documents and Dashboards
- Use Dashboards Sparingly: Only include dashboards where interactivity adds value (e.g., for data exploration or detailed analysis).
- Optimize for Performance: Large interactive elements or datasets may slow down rendering. Optimize your data and visuals for quick loading times.
- Keep It Simple: While Shiny allows for rich interactivity, ensure your dashboards remain intuitive and easy to navigate.
- Include Contextual Information: Accompany interactive elements with adequate explanations, so users know how to interact with the dashboard and understand the visualized data.
Conclusion
With Quarto, you can easily create comprehensive reports that combine static documents with dynamic, interactive dashboards. Whether you need multi-output reports (PDF, HTML, Word) or wish to incorporate interactive dashboards for data exploration, Quarto provides the flexibility and power to streamline and enhance your reporting workflow.
In this post, we covered:
- Multi-format reports: Outputting in PDF, HTML, and Word from a single source.
- Interactive dashboards: Embedding Shiny apps and interactive elements for deeper engagement.