8000 GitHub - mrcaseb/gtExtras: A Collection of Helper Functions for the gt Package.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

mrcaseb/gtExtras

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gtExtras

The goal of gtExtras is to provide some additional helper functions to assist in creating beautiful tables with {gt}

Installation

You can install the dev version of gtExtras from GitHub with:

remotes::install_github("jthomasmock/gtExtras")

fmt_symbol_first

This function allows you to format your columns only on the first row, where remaining rows in that column have whitespace added to the end to maintain proper alignment.

library(gtExtras)
library(gt)

gtcars %>%
  head() %>%
  dplyr::select(mfr, year, bdy_style, mpg_h, hp) %>%
  dplyr::mutate(mpg_h = rnorm(n = dplyr::n(), mean = 22, sd = 1)) %>%
  gt::gt() %>%
  gt::opt_table_lines() %>%
  fmt_symbol_first(column = mfr, symbol = "$", suffix = " ", last_row_n = 6) %>%
  fmt_symbol_first(column = year, symbol = NULL, suffix = "%", last_row_n = 6) %>%
  fmt_symbol_first(column = mpg_h, symbol = "%", suffix = NULL, last_row_n = 6, decimals = 1) %>%
  fmt_symbol_first(column = hp, symbol = "°", suffix = "F", last_row_n = 6, decimals = NULL, symbol_first = TRUE)

pad_fn

You can use pad_fn() with gt::fmt() to pad specific columns that contain numeric values. You will use it when you want to “decimal align” the values in the column, but not require printing extra trailing zeroes.

data.frame(x = c(1.2345, 12.345, 123.45, 1234.5, 12345)) %>%
  gt() %>%
  fmt(fns = function(x){pad_fn(x, nsmall = 4)}) %>%
  tab_style(
    # MUST USE A MONO-SPACED FONT
    style = cell_text(font = google_font("Fira Mono")),
    locations = cells_body(columns = x)
    )

Themes

The package includes two different themes, the gt_theme_538() styled after FiveThirtyEight style tables, and gt_theme_espn() styled after ESPN style tables.

head(mtcars) %>%
  gt() %>% 
  gt_theme_538()

head(mtcars) %>%
  gt() %>% 
  gt_theme_538()

Hulk data_color

This is an opinionated diverging color palette. It diverges from low to high as purple to green. It is a good alternative to a red-green diverging palette as a color-blind friendly palette. The specific colors come from colorbrewer2.

Basic usage below, where a specific column is passed.

# basic use
head(mtcars) %>%
  gt::gt() %>%
  gt_hulk_color(mpg)

Trim provides a tighter range of purple/green so the colors are less pronounced.

head(mtcars) %>%
  gt::gt() %>%
  # trim gives smaller range of colors
  # so the green and purples are not as dark
  gt_hulk_color(mpg:disp, trim = TRUE) 

Reverse makes higher values represented by purple and lower by green. The default is to have high = green, low = purple.

# option to reverse the color palette
# so that purple is higher
head(mtcars) %>%
  gt::gt() %>%
  # reverse = green for low, purple for high
  gt_hulk_color(mpg:disp, reverse = FALSE) 

About

A Collection of Helper Functions for the gt Package.

Resources

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • R 100.0%
0