Skip to contents

Divides the selected columns and records the scaling in the same call, so the numbers and the disclosure cannot drift apart.

Usage

gt_scale_note(
  gt_object,
  columns,
  divisor = 1000,
  note = NULL,
  where = c("source_note", "label", "both"),
  label_suffix = NULL,
  decimals = 0,
  ...
)

Arguments

gt_object

A gt table object to modify.

columns

The column or columns to scale.

divisor

Numeric. The amount to divide by. Defaults to 1000.

note

Optional. The disclosure text. If NULL, it is derived from divisor, giving "Figures in thousands." for 1e3, "Figures in millions." for 1e6, and so on. Divisors without a common name fall back to "Figures divided by 2,500." Defaults to NULL.

where

Character. Where the disclosure goes. One of "source_note", "label" to append a suffix to the column labels instead, or "both". Defaults to "source_note".

label_suffix

Optional. The suffix appended to column labels when where includes "label". If NULL, it is derived from divisor (for example, "(000s)"). Defaults to NULL.

decimals

Integer. The number of decimal places for the scaled values. Defaults to 0.

...

Additional arguments passed to gt::fmt_number.

Value

Returns a modified gt table with the columns scaled and the scaling disclosed.

Details

Scaling is applied through gt::fmt_number() and its scale_by argument, so the underlying data is left untouched.

Examples

library(gt)

# an NBA season's points, scored and allowed
atlantic <- subset(
  sdv_example_standings, division == "Atlantic",
  c(team_name, points_for, points_against)
)

# Boston's 9,418 points render as 9.4, with "Figures in thousands." beneath
gt(atlantic) |>
  gt_scale_note(c(points_for, points_against), divisor = 1e3, decimals = 1)
team_name points_for points_against
Boston Celtics 9.4 8.8
New York Knicks 9.5 9.0
Toronto Raptors 9.4 9.2
Philadelphia 76ers 9.5 9.5
Brooklyn Nets 8.7 9.5
Figures in thousands.
# disclose in the column labels instead gt(atlantic) |> gt_scale_note(c(points_for, points_against), divisor = 1e3, decimals = 2, where = "label" )
team_name points_for (000s) points_against (000s)
Boston Celtics 9.42 8.79
New York Knicks 9.55 9.03
Toronto Raptors 9.40 9.17
Philadelphia 76ers 9.50 9.52
Brooklyn Nets 8.69 9.51