rr-markdownsummarytools

R summarytools dfSummary not displaying correctly in RMarkdown tabs


I'm trying to put 2 dfSummary outputs into a tabbed section in RMarkdown. I think I have to use results = 'asis' in order for the plots to generate, but when I use that option the tab functionality breaks. Instead of appearing in tabs, the summaries are shown in-line in the document.

The example below is on the ToothGrowth dataset, and shows the issue I'm hoping to fix.

---
title: "test"
output: html_document
---

```{r setup, include = FALSE}
library(summarytools)

st_options(plain.ascii = FALSE               # This is very handy in all Rmd documents
         , style = "rmarkdown"               # This too
         , footnote = NA                     # Avoids footnotes which would clutter the results
         , subtitle.emphasis = FALSE         # This is a setting to experiment with - according to the theme used, it might improve the headings layout
)
           
knitr::opts_chunk$set(results = 'asis')
```

## R Markdown

### {.tabset .tabset-pills}  

#### Numeric Variables  

This section is for numeric variables.  

```{r}
summarytools::dfSummary(ToothGrowth[c('len', 'dose')], style = "grid", graph.magnif = 0.75, valid.col = FALSE, tmp.img.dir = "/tmp")
```

#### Factor Variables  

This section is for factor variables.  

```{r}
summarytools::dfSummary(ToothGrowth[c('supp')], style = "grid", graph.magnif = 0.75, valid.col = FALSE, tmp.img.dir = "/tmp")
```

Thanks!


Solution

  • I would try those two things:

    1. Setting headings=FALSE. This will prevent ##'s conflicting with the tabbed content inner workings.
    2. Using # {.tabset .tabset-pills} and then ## Numeric & ## Factor. This could be a viable alternative to the first solution, as it uses different heading levels than summarytools'.