Kathryn Dalton
Kathryn Dalton

Reputation: 1

summarytools dfSummary markdown not printing variable labels

I am trying to get my variable labels to print on my dfSummary Table in my markdown output. I can get the label column when I use the R console viewer (with print(method="viewer")), but when I knit the markdown the label column drops off. See an example of my problem using the mtcars dataset

knitr::opts_chunk$set(results = "asis")
library(Hmisc)
library(summarytools)
st_options(plain.ascii = FALSE,       
           style        = "rmarkdown", 
           footnote     = NA,          
           subtitle.emphasis = FALSE) 
label(mtcars$gear) ="Gears"
label(mtcars$cyl) ="Cylinders"
mt<-subset(mtcars, select=c(gear, cyl))
print(dfSummary(mt, labels.col=TRUE, display.labels=TRUE), method = "render", Variable.label=TRUE)

This is the resulting table in the markdown output

table in markdown

enter image description here

BUT this is what I want it to look like, with the Label column, which is what I get when I enter the code directly into RConsole:

print(dfSummary(mt, labels.col=TRUE, display.labels=TRUE), method = "viewer", Variable.label=TRUE)

table using R viewer

enter image description here I'd like to get the "Label" column on the rmarkdown table - can anyone help with this issue?

Upvotes: 0

Views: 835

Answers (1)

Dominic Comtois
Dominic Comtois

Reputation: 10431

The labels are lost when you use subset. Just apply the labels after subsetting.

Also, since summarytools has its own label() function, you don't need to load Hmisc for that.

Here's how I got it to display optimally:

---
title: "Labels in dfSummary"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(results = "asis")
library(summarytools)
st_options(plain.ascii     = FALSE,       
           dfSummary.style = "grid",
           dfSummary.graph.magnif = .75,
           footnote        = NA) 

```

```{r, echo=FALSE}
st_css()
```

```{r}
mt <- subset(mtcars, select=c(gear, cyl))
label(mt$gear) ="Gears"
label(mt$cyl) ="Cylinders"
print(dfSummary(mt), method = "render")
```

rendered results

Upvotes: 0

Related Questions