T1802173
T1802173

Reputation: 113

How to loop through a dataframe to create a table of contents in quarto

I have a data frame df_full that I want to loop through to create a table of contents in quarto. I want each heading to be unique(df_full$Topic), each subheading to be unique(df_full$question_id) within each topic, and the content displayed to be unique(df_full$response).

My attempt is this:

---
title: "PSP ESG and Impact Orientation QAQC"
format:
  html:
    toc: true
    toc-depth: 2
    number-sections: true
    toc-location: left
    css: corp-styles_2023-new-logo.css
    self-contained: true
editor: visual
execute:
  echo: false
  warning: false
editor_options:
  chunk_output_type: console
---
# Loop through unique topics
for (topic in unique(df_full$Topic)) {
  cat("#", topic, "\n")  # Main section for each Topic

  # Filter to get questions and responses for the current topic
  topic_data <- df_full |>  
    filter(Topic == topic)

  for (i in 1:nrow(topic_data)) {
    # Use Question as a subsection
    cat("## Question:", topic_data$Question[i], "\n") 
    # Add the dummy response
    cat("### Response:", topic_data$dummy_response[i], "\n") 
  }
}

but the results just display as text like this:

# ESG Governance and Disclosure 
## Question: Formal ESG policy or policies on relevant environmental, social, governance topics? 
### Response: Yes, policy or strategy in place 
## Question: Formal ESG policy or policies on relevant environmental, social, governance topics? 
### Response: Yes, policy or strategy in place 
## Question: Formal ESG policy or policies on relevant environmental, social, governance topics? 
### Response: Yes, policy or strategy in place

Upvotes: 0

Views: 92

Answers (1)

margusl
margusl

Reputation: 17554

If your code generates raw Markdown and you want to keep it as-is, you should also set output (or results) option accordingly, e.g. :

```{r}
#| output: asis

cat("# Topic\n")
```

Complete qmd for reference:

---
format:
  html:
    toc: true
    toc-depth: 2
    number-sections: true
    toc-location: left
    self-contained: true
editor: visual
execute:
  echo: false
  warning: false
editor_options:
  chunk_output_type: console
---
```{r}
#| label: setup

library(dplyr)
df_full <- tribble(
  ~Topic, ~Question, ~dummy_response,
  "A", "B", "C"
)
```

Code-block with `output: asis`:
```{r}
#| label: with-output-asis
#| output: asis

# Loop through unique topics
for (topic in unique(df_full$Topic)) {
  cat("#", topic, "\n")  # Main section for each Topic

  # Filter to get questions and responses for the current topic
  topic_data <- df_full |>  
    filter(Topic == topic)

  for (i in 1:nrow(topic_data)) {
    # Use Question as a subsection
    cat("## Question:", topic_data$Question[i], "\n") 
    # Add the dummy response
    cat("### Response:", topic_data$dummy_response[i], "\n") 
  }
}
```
---
Code-block without `output: asis` :

```{r}
#| label: without-output-asis

cat("#", topic, "\n") 
cat("## Question:", topic_data$Question[i], "\n") 
cat("### Response:", topic_data$dummy_response[i], "\n") 
```

Renders as:

rendered qmd

Upvotes: 0

Related Questions