Search code examples
rloopsmarkdownquarto

Quarto, how to generate tabset-paned programmatically to print code output not plots


Following this link I can generate tabsets of plots programmatically in Quarto as follows (so useful!):

---
title: "quarto_test_pad"
---

# Plot list

Code to make some plots. The plotting part is all good!

```{r}
#| echo: false

# A list of plots:
library(palmerpenguins)
library(ggplot2)
plot_list <- penguins |>
  split(penguins$species) |>
  lapply(function(x){
    ggplot(x, aes(x = flipper_length_mm, y = body_mass_g)) + 
      geom_point() +
      theme_minimal()
  }) 

headings <- names(plot_list)

```


## Plot tabs

Code to display plots programmatically. So far so good!

:::{.panel-tabset}
```{r, results='asis'}
#| warning: false
for (i in seq_along(plot_list)) {
  cat("# ",headings[i],"\n")
  print(plot_list[[i]])
  cat('\n\n')
}
```
:::

I would like to also make tabsets that print out the summary of a bunch of models. Having #| results: 'asis' prints the summary as text rather than the original code output.

# Summary list

Code to make a bunch of example models.

```{r}
#| echo: false

# A list of summaries:
library(palmerpenguins)

summary_list <- penguins |>
  split(penguins$species) |>
  lapply(function(dat){
    lm(flipper_length_mm ~  body_mass_g, data = dat)
  }) 

headings <- names(summary_list)

```


## Summary tabs

Ok, here is the problem. With `results='asis'` it doesn't display the code output as the formatted code output but as text. Actually with `summary` it won't show anything... but it does with `print` :D.

:::{.panel-tabset}
```{r, results='asis'}
#| warning: false
for (i in seq_along(summary_list)) {
  cat("# ",headings[i],"\n")
  summary(summary_list[[i]])
  cat('\n\n')
}
```
:::

What I would like to achieve is the raw original model summary output in tabsets. Since I have a lot more plots and summaries than in this reprex I do need a programmatic solution. I'm not using the lm() function in my actual project so broom or DT probably won't work...


# What I would like in each tab:

is the output printed as raw code:

```{r}
summary(summary_list[[1]])
summary(summary_list[[2]])
summary(summary_list[[3]])

```

TIA! Quinn

Also looked into:

Programatically generate tabset panels of datatables in Quarto


Solution

  • Wrap the output of summary with three backticks using cat to get the code-text instead of simple text inside the panel tabsets.

    ---
    title: "Programmatically Generating Panel Tabsets"
    format: html
    ---
    
    # Summary list
    
    Code to make a bunch of example models.
    
    ```{r}
    #| echo: false
    
    # A list of summaries:
    library(palmerpenguins)
    
    summary_list <- penguins |>
      split(penguins$species) |>
      lapply(function(dat){
        lm(flipper_length_mm ~  body_mass_g, data = dat)
      }) 
    
    headings <- names(summary_list)
    
    ```
    
    ## Summary tabs
    
    :::{.panel-tabset}
    
    ```{r, results='asis'}
    #| warning: false
    for (i in seq_along(summary_list)) {
      cat("# ",headings[i],"\n")
      cat("```\n")
      print(summary(summary_list[[i]]))
      cat("\n```\n")
      cat('\n\n')
    }
    ```
    
    :::