Study Notes: gt package and format table

A introduction about gt package is here

knitr::opts_chunk$set(echo = TRUE, 
                      warning = FALSE, 
                      message = FALSE,
                      fig.align = "default", 
                      eval = TRUE)

library(gt)
## Warning: package 'gt' was built under R version 4.1.2
suppressMessages(library(tidyverse))
## Warning: package 'dplyr' was built under R version 4.1.2

Basics of gt

A basic gt table can be created as so

data("iris")
glimpse(iris)
## Rows: 150
## Columns: 5
## $ Sepal.Length <dbl> 5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9, 5.4, 4.…
## $ Sepal.Width  <dbl> 3.5, 3.0, 3.2, 3.1, 3.6, 3.9, 3.4, 3.4, 2.9, 3.1, 3.7, 3.…
## $ Petal.Length <dbl> 1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5, 1.4, 1.5, 1.5, 1.…
## $ Petal.Width  <dbl> 0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.3, 0.2, 0.2, 0.1, 0.2, 0.…
## $ Species      <fct> setosa, setosa, setosa, setosa, setosa, setosa, setosa, s…
iris %>% 
  head() %>% 
  gt()

Sepal.LengthSepal.WidthPetal.LengthPetal.WidthSpecies
5.13.51.40.2setosa
4.93.01.40.2setosa
4.73.21.30.2setosa
4.63.11.50.2setosa
5.03.61.40.2setosa
5.43.91.70.4setosa

You can add row names (rowname_col argument) and add group names (groupname_col argument) into the table:

iris %>% 
  arrange(desc(Sepal.Length)) %>% # 6 types of iris with largest sepal length
  mutate(Rank = paste0("ID", 1:nrow(.))) %>% 
  head(20) %>% 
  gt(groupname_col = "Species", 
     rowname_col = "Rank")

Sepal.LengthSepal.WidthPetal.LengthPetal.Width
virginica
ID17.93.86.42.0
ID27.73.86.72.2
ID37.72.66.92.3
ID47.72.86.72.0
ID57.73.06.12.3
ID67.63.06.62.1
ID77.42.86.11.9
ID87.32.96.31.8
ID97.23.66.12.5
ID107.23.26.01.8
ID117.23.05.81.6
ID127.13.05.92.1
ID156.93.25.72.3
ID166.93.15.42.1
ID176.93.15.12.3
ID196.83.05.52.1
ID206.83.25.92.3
versicolor
ID137.03.24.71.4
ID146.93.14.91.5
ID186.82.84.81.4

Next, the boarder could be added into the table:

iris %>% 
  arrange(desc(Sepal.Length)) %>% # 6 types of iris with largest sepal length
  mutate(Rank = paste0("ID", 1:nrow(.))) %>% 
  head(20) %>% 
  gt(groupname_col = "Species", 
     rowname_col = "Rank") %>% 
  ########################### 
  # Below is changed
  ###########################
  tab_style( # tab_style to change style of cells, 
    # cells_borders provides the formatting
    # locations tells it where add black borders to all column labels
    style = list(
      cell_borders(
        sides = "left",
        color = "black",
        weight = px(1.2)
      )
    ),
    locations = list(
      cells_body(
        columns = colnames(iris)
      )
    )
  ) %>% 
  # Add botton line below the column names
  tab_style(
    style = list(
      cell_borders(
        sides = "bottom",
        color = "black",
        weight = px(3)
      )
    ),
    locations = list(
      cells_column_labels(
        columns = gt::everything()
      )
    )
  )

Sepal.LengthSepal.WidthPetal.LengthPetal.Width
virginica
ID17.93.86.42.0
ID27.73.86.72.2
ID37.72.66.92.3
ID47.72.86.72.0
ID57.73.06.12.3
ID67.63.06.62.1
ID77.42.86.11.9
ID87.32.96.31.8
ID97.23.66.12.5
ID107.23.26.01.8
ID117.23.05.81.6
ID127.13.05.92.1
ID156.93.25.72.3
ID166.93.15.42.1
ID176.93.15.12.3
ID196.83.05.52.1
ID206.83.25.92.3
versicolor
ID137.03.24.71.4
ID146.93.14.91.5
ID186.82.84.81.4
Jihong Zhang, M.S.
Jihong Zhang, M.S.
Ph.D. Candidate

My research interests mainly focus on the Bayesian Diagnostic Classification Models (DCMs) - a special kind of Item Response Model and the model checking method, as applied in the psychological, educational, and social sciences. I seek to improve the utility of advanced psychometric modeling and provide easy-to-use tools or software for researchers.