Search code examples
rsummarygtsummarytbl

How can I indent specified rows when using tbl_summary from the gtsummary package?


I have a composite variable ("Needs Medical Attention") that is made up of several sub-variables ("Heart Disease", "Cancer"); that is, if a subject has "Heart Disease" or "Cancer", they are flagged for "Needs Medical Attention". In my presentation of these variables, I would like to visually suggest that "Heart Disease" and "Cancer" are sub-categories of "Needs Medical Attention".

In the past, I have used kableExtra() to print tables where I'm then able to add indents to certain rows using add_indent(c(rows_that_want_indented)). I'd love an equivalent for tbl_summary, but I'm unable to.

I've tried just adding spaces (" ") to the beginning of my variable labels to blunt force it, but these empty spaces aren't recognized. I've also tried using "&nbsp" and a bunch of its varieties with various numbers of backslashes (\), but with no luck.

library("tidyverse")
library("gtsummary")

sim_data <- data.frame(
  number = rnorm(20),
  heart_disease = c(rep(0, 10), rep(1, 10)),
  cancer = c(rep(1, 5), rep(0, 5), rep(1, 5), rep(0, 5))
)

sim_data$need_medical_attention <- with(sim_data, ifelse(heart_disease == 1 | cancer == 1, 1, 0))


tbl_summary(data = sim_data,
            include = c(
              number,
              need_medical_attention,
              heart_disease,
              cancer
            ),
            label = list(
              number = "Number",
              need_medical_attention = "Needs Medical Attention",
              heart_disease = "    Heart disease",
              cancer = "&nbspCancer"
            ))

current plot

Lastly, I've also messed around with modify_table_styling() using text_format = "indent", but I think this just refers to rows that have sub-rows that are indented (like rows with continuous statistical summaries, i.e. mean and variance or median and IQR).

How can I indent these rows?

(Note: I am not dealing with categories of categorical variables that need to be indented. I'm dealing with entire variables).


Solution

  • Here's a working example for you. Happy Programming!

    library(gtsummary)
    #> #BlackLivesMatter
    library(tidyverse)
    packageVersion("gtsummary")
    #> [1] '1.7.2'
    set.seed("11235")
    
    sim_data <- 
      tibble(
        number = rnorm(20),
        heart_disease = c(rep(0, 10), rep(1, 10)),
        cancer = c(rep(1, 5), rep(0, 5), rep(1, 5), rep(0, 5)),
        need_medical_attention = pmax(heart_disease, cancer)
      ) |> 
      relocate(need_medical_attention, .after = number)
    
    tbl <-
      sim_data |> 
      tbl_summary(
        label = list(number = "Number",
                     need_medical_attention = "Needs Medical Attention",
                     heart_disease = "Heart disease",
                     cancer = "Cancer")
      ) |> 
      modify_column_indent(
        columns = label, # use `show_header_names()` to get a printout of the underlying column names
        rows = variable %in% c("cancer", "heart_disease") # show the full data frame available by looking at `.$table_body`. There is a column called variable we can utilize for this
      )
    

    enter image description here Created on 2023-07-19 with reprex v2.0.2

    Not exactly what you're looking for today, but this function could also help you out in the future. https://mskcc-epi-bio.github.io/bstfun/reference/add_variable_grouping.html