Skip to content

Implementing dynamic figure captions for multiple heatmaps within a loop in a Quarto document. #11852

@Liu499-create

Description

@Liu499-create

Issue Description

Background

I am using Quarto to generate a report where I need to dynamically generate multiple heatmaps within a single R code block using the purrr::iwalk loop. My goal is to set dynamic figure captions for each heatmap based on variables within the loop. I tried using the fig-cap parameter in combination with the glue::glue function to dynamically generate captions, but this method seems to only work for the first heatmap in the loop and does not support multiple figures within the same loop.

Attempted Solutions

I have tried the following methods to achieve dynamic figure captions:

  1. Using glue::glue within fig-cap to reference loop variables, hoping to generate different captions for each figure. For example:

    #| fig-cap: !expr glue::glue("{names(descrips)}pheatmap")

    This method only works for the first figure in the loop, and captions for subsequent figures do not display correctly.

  2. Manually using the cat() function within the loop to output titles. Although this method allows for accurate descriptions, it does not integrate into the properties of the figure files and does not align with my purpose of using fig-cap.

Problem

How can I dynamically set fig-cap for each figure within a loop in a Quarto document? Is there a recommended method or an upcoming feature update that supports this use case?

Expected Solution

I hope to be able to set individual, dynamically generated figure captions in Quarto, especially when using loops or similar dynamic structures. If the current version of Quarto does not support this, I would like to know if there are plans to include this functionality in future updates.


the following is my code

#| label: fig-heatmap-and-statistical-description
#| fig-cap: !expr glue::glue("{names(descrips)}, Hierarchical Clustering Heatmaps")
#| results: 'asis'
#| fig-width: 10
#| fig-height: 6

  # Monthly disease list - Complete year/month heatmaps
  pheatmap_df <- dyq_cstd |>
    dyq_complete_yearmonth(analysis_start_year = analysis_start_year, 
      analysis_end_year = analysis_end_year,
      analysis_quarter = analysis_quarter,
      analysis_disease = analysis_disease)

  iwalk(descrips, ~{
      cat("###  ", .y, "\n\n", sep="")

      cat(.x$year_range, .y, " incidence rate remains high. Reported incidence rates are between ", .x$incidence_range, " per 100,000, with an average annual incidence of ", .x$average_incidence, " per 100,000. The highest annual incidence was in ", .x$highest_incidence_year, ", and the lowest was in ", .x$lowest_incidence_year, ", see ", '@fig-key-disease-incidence-combination-chart', "\n\n")

      # Use plot_heatmap to draw the monthly heatmap
      pheatmap_df[[.y]] |> 
        # When save_path is set to NULL, it is used for output in Quarto
        plot_heatmap(.y, save_path=NULL)
      
      cat("\n\n")
        
  })

Metadata

Metadata

Assignees

No one assigned

    Labels

    supporta request for support

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions