Skip to contents

This helper function adds whitespace to numeric values so that they can be aligned on the decimal without requiring additional trailing zeroes. This function is intended to use within the gt::fmt() function.

Usage

pad_fn(x, nsmall = 2, pad0)

Arguments

x

A vector of numbers to pad/align at the decimal point

nsmall

The max number of decimal places to round at/display

pad0

A logical, indicating whether to pad the values with trailing zeros.

Value

Returns a vector of equal length to the input vector

Figures

Function ID

2-3

Examples


library(gt)
padded_tab <- 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
    # https://fonts.google.com/?category=Monospace
    style = cell_text(font = google_font("Fira Mono")),
    locations = cells_body(columns = x)
  )