change column labels in gt table

Viewed 178

I would like to quickly change the column labels in a gt table of several models. Is there an easier way than providing a named vector? It would be nice to just provide a vector of names.

data('mtcars')
str(mtcars)
mod1<-lm(mpg~wt, data=mtcars)
mod2<-lm(hp~wt, data=mtcars)
#install.packages('modelsummary')
library(modelsummary)
mods<-modelsummary(list(mod1, mod2), output="gt")
#install.packages('gt')
library(gt)
#install.packages('tidyverse')
library(tidyverse)
mods %>% 
cols_label("Model 1" = 'mpg', "Model 2"='hp')
1 Answers

You can pass your vector of names to the setNames function before calling modelsummary. This will name your list of models. For example:

library(modelsummary)
library(tidyverse)

mod1 <- lm(mpg~wt, data=mtcars)
mod2 <- lm(hp~wt, data=mtcars)

list(mod1, mod2) %>%
  setNames(c("Model A", "Model B")) %>%
  modelsummary(output = "gt")
Related