I am using RStudio. I want to generate headers and plots inside a for loop. But it does not work.
My code should generate three headers. One for each region_value. But I only obtain the header for the first region value.
What is wrong in my code?
---
title: "Generate Headers"
format: html
editor: visual
---
## Generate dataframe
```{r}
#| echo: false
#| warning: false
library(tidyverse)
region = c(replicate(100,"A"),replicate(100,"B"),replicate(100,"C"))
variable = c(replicate(50,"var_a"),replicate(50,"var_b"),replicate(50,"var_a"),replicate(50,"var_b"),replicate(50,"var_a"),replicate(50,"var_b"))
value = runif(300, min = 0, max = 100)
df = data.frame(region = region, variable = variable, value = value)
```
# Plots
```{r}
#| echo: false
#| results: asis
regions_values <- unique(df$region)
for (region_value in regions_values){
cat("## ", region_value, "\n")
dfplot <- df |> filter(region == region_value)
p <- ggplot(dfplot, aes(x=variable, y=value)) + geom_boxplot()
print(p)
cat("\n")
}
```
