Programmatically create section heading with rmarkdown

Hi,

I would like to know if it is possible and what is the best way to programmatically create html section headings in a Rmarkdown code chuck. Specifically, I am trying to create section headings and plots dynamically within a loop. The example below illustrates a loop with 3 iterations, but in actuality, my code will exist in a Rmarkdown template which will process data of variable size and content so the number of iterations and headings is a priori unknown.

Thanks in advance for your help


title: "Untitled"
author: "Unknown"
date: "2024-10-17"
output: html_document

knitr::opts_chunk$set(echo = TRUE)

Intro

Hello world!

Analysis

required(ggplot2)
for (i in 1:3) {
  message( paste("Iteration", i) )
  ggplot(diamonds, aes(carat, price)) + geom_point()
}