rgtsummarygt

How do I target the `gtsummary::as_gt()` columns in `gt` styling functions?


I use gtsummary::as_gt() to style summary tables using gt functions. For styling functions such as gt::cols_width() and gt::tab_style(), one needs to know how the columns of the table are named. This is a follow-up question to this question.

However, the documentation of as_gt() does not contain any information on how the converted columns are named. Using a mixture of trial and error and looking at the resulting gt_tbl table object, I tried to find out how the Characteristic columns from the tbl_regression() is targeted. I believe it is named variable or var_label but neither works.

For example, the following code has no effect on the column width.

---
title: "Untitled"
author: "test"
date: "`r Sys.Date()`"
output:
  pdf_document:
    keep_tex: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(gt)
```

```{r}
library(gtsummary)
data(trial)
attr(trial$stage, "label") <- "Treatment stage [This is a long explanation that cannot go anywhere else.]"
m1 <- glm(response ~ age + stage, trial, family = binomial)
m1 %>%
  tbl_regression(exponentiate = TRUE) |>  
  as_gt() |> 
  cols_width(variable ~ px(100)) |> 
  as_latex()
```

On the other hand, switching to cols_width(everything() ~ px(100)) works, but I would like to target columns specifically.

Note:

Meddling with LaTeX as suggested in the other question works (see code below), but it would be nice to have an elegant solution within gt.

```{r}
# ... |>
  as_latex() -> latex_table

latex_table[[1]] <- sub(
  "\\begin{longtable}{lccc}", 
  "\\begin{longtable}{p{4.5cm}ccc}", 
  latex_table[[1]], 
  fixed = TRUE
)

latex_table
```

Solution

  • In this particular case you might make use of the gtsummary::show_header_names() function in order to ascertain the column names.

    From ?gtsummary::show_header_names:

    Use show_header_names() to learn the column names.

    library(gt)
    library(gtsummary)
    
    data(trial)
    
    attr(trial$stage, "label") <- 
      "Treatment stage [This is a long explanation that cannot go anywhere else.]"
    
    m1 <- glm(response ~ age + stage, trial, family = binomial)
    
    
    m1 %>%
      tbl_regression(exponentiate = TRUE) |> 
      show_header_names()
    #> ℹ As a usage guide, the code below re-creates the current column headers.
    #> modify_header(
    #>   label = "**Characteristic**",
    #>   estimate = "**OR**",
    #>   ci = "**95% CI**",
    #>   p.value = "**p-value**"
    #> )
    #> 
    #> 
    #> Column Name   Column Header      
    #> ------------  -------------------
    #> label         **Characteristic** 
    #> estimate      **OR**             
    #> ci            **95% CI**         
    #> p.value       **p-value**
    

    Then we can target specific columns by name. Here I target the label column by making it either narrow or wide.

    Narrow label column

    m1 %>%
      tbl_regression(exponentiate = TRUE) |>
      as_gt() |> 
      cols_width(label ~ px(100)) |>
      as_latex()
    

    enter image description here

    Wide label column

    m1 %>%
      tbl_regression(exponentiate = TRUE) |>
      as_gt() |>
      cols_width(label ~ px(325)) |>
      as_latex()
    

    enter image description here

    A word of caution

    TL;DR: gtsummary::show_header_names() is for gtsummary objects and not gt_tbl objects in general.

    As a general warning, one needs to be a bit mindful of the object classes in use because while the {gtsummary} package extends the {gt} package, the associated objects and functions are not always interchangeable.

    Note that gtsummary::tbl_regression() outputs objects of type c("tbl_regression", "gtsummary").

    m1 %>%
      tbl_regression(exponentiate = TRUE) |>
      class()
    #> [1] "tbl_regression" "gtsummary"
    

    And gt::as_gt() outputs objects of type c("gt_tbl", "list").

    m1 %>%
      tbl_regression(exponentiate = TRUE) |>
      as_gt() |>
      class()
    #> [1] "gt_tbl" "list"
    

    Therefore this works:

    m1 %>%
      tbl_regression(exponentiate = TRUE) |> 
      show_header_names()
    #> ℹ As a usage guide, the code below re-creates the current column headers.
    #> modify_header(
    #>   label = "**Characteristic**",
    #>   estimate = "**OR**",
    #>   ci = "**95% CI**",
    #>   p.value = "**p-value**"
    #> )
    #> 
    #> 
    #> Column Name   Column Header      
    #> ------------  -------------------
    #> label         **Characteristic** 
    #> estimate      **OR**             
    #> ci            **95% CI**         
    #> p.value       **p-value**
    

    But this does not:

    m1 %>%
      tbl_regression(exponentiate = TRUE) |>
      as_gt() |> 
      show_header_names()
    #> Error: Error in argument 'x='. Expecting object of class 'gtsummary'
    

    Created on 2024-07-12 with reprex v2.1.0.9000

    Reprex files hosted with on GitHub