Quarto Report with repeating sections

Hi. I have wat I think should be a fairly common scenario. Its certainly something we need to solve for creating enterprise reports using Quarto and Connect.

I want to use quarto to create a fairly complex report. At the moment output format isn't important to me and I'm just working with a html report, but at the end I suppose i would ideally have revealjs slides.

My report consists of a summary section and then a repeated section. In the attached example, the repeated section runs once for each of the penguin species.

The repeated section is a combination of text output, text tables, and charts. I want the charts to be plotly charts so i can use tooltips and and the plotly controls to highlights things in a meeting reviewing the report.

I don't want separate reports. I want a single report. I would be fine creating multiple reports and then combining them, but I don't see a way to do that either.

Please see attached example. Any suggestions are appreciated.


title: "quarto repeating section question"
format: html
editor: visual

Question

I have a report document that has a repeating section. in the repeating section i want a text table and a chart that repeats. the chart needs to be a plotly chart because i need the interactivity i can't get with a ggplot object.

i can't for the life of me figure out how to make this work!!!

#| label: package-loading-and-get-data

library(tidyverse)
library(gt)
library(ggplot2)
library(plotly)

df <- palmerpenguins::penguins

penguin_list <- unique(df$species)


create_table <- function(df_in) {
  
  t <- select(df, species, sex, body_mass_g, flipper_length_mm)
  
  table <- t |> 
    group_by(species, sex) |> 
    summarize(mass_avg = mean(body_mass_g),
              length_avg = mean(flipper_length_mm),
              .groups= "drop") |> 
    gt()
  
  return(table)
  
}

create_plot <- function(df_in) {
  
  plot <- ggplot(df_in, aes(x = body_mass_g, y = flipper_length_mm)) +
    geom_point(stat = "identity")
  
  plot <- ggplotly(plot)
  
  return(plot)
}

#| label: non-repeating-section

df |> 
  group_by(species) |> 
  summarize(n = n()) |> 
  gt()


#| label: example-without-loop

create_table(df)

create_plot(df)

#| label: with-loop
#| output: asis

plotlist = list()

for (p in penguin_list) {
  
  print(paste0("species: ", p))
  
  loop_df <- df |> 
    dplyr::filter(species == p)
  
  print(create_table(loop_df))
  
  fig <- plot_ly(data = loop_df, x = ~body_mass_g, y = ~flipper_length_mm)
  
  fig
  
  # pl <- create_plot(loop_df)
  # plotlist[[i]] = pl
  # htmltools::html_print(pl)
  
  print("end of loop run")
}

First, please do format your code correctly. That will be easier to read:

FAQ: How to Format R Markdown Source

With Quarto you can include some external .qmd content this way: Quarto - Includes

Otherwise, you need to consider knitr feature with child document. We share an example and links to some docs at: Create tabsets panel from R code
This is to dynamically create tabset but you should be able to adapt to your usage.

Hope it helps

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.