Skip to content

How to create different sized figures in programmatically-generated markdown sections #11042

Closed Answered by wvictor14
wvictor14 asked this question in Q&A
Discussion options

You must be logged in to vote

Thank you @mcanouil

This is a solution to above using knitr::knit_child()

---
title: "Dynamically creating sections with multiple plots"
format: 
  html:
    toc: true
    keep-md: true
engine: knitr
editor_options: 
  chunk_output_type: console
---

```{r}
library(dplyr)
library(ggplot2)
library(purrr)
sections <- mtcars$cyl |>  unique() |>  sort()
```


```{r, echo=FALSE, results='asis'}
res <- purrr::map(sections, function(x) {
  knitr::knit_child(text = c(
    '## Plot "`r x`"',
    '',
    '```{r}',
    '#| fig-height: 10',
    'p <- mtcars |> 
        ggplot(aes(x = disp, y = hp)) +
        geom_point() +
        labs(title = x)
     plot(p)',
     '```',
    '',
    
    '```{r}',

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by wvictor14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
2 participants