Learn R Programming

gtExtras (version 0.5.0)

pad_fn: Pad a vector of numbers to align on the decimal point.

Description

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)

Value

Returns a vector of equal length to the input vector

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.

Figures

Function ID

2-3

See Also

Other Utilities: add_text_img(), fa_icon_repeat(), fmt_pad_num(), fmt_pct_extra(), fmt_symbol_first(), generate_df(), gt_add_divider(), gt_badge(), gt_double_table(), gt_duplicate_column(), gt_fa_column(), gt_fa_rank_change(), gt_fa_rating(), gt_fa_repeats(), gt_highlight_cols(), gt_highlight_rows(), gt_img_border(), gt_img_circle(), gt_img_multi_rows(), gt_img_rows(), gt_index(), gt_merge_stack_color(), gt_merge_stack(), gt_two_column_layout(), gtsave_extra(), img_header(), tab_style_by_grp()

Examples

Run this code

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)
  )

Run the code above in your browser using DataLab