This vignette shows how to decorate columns for custom formatting. We use the formattable package for demonstration because it already contains useful vector classes that apply a custom formatting to numbers.
The x column in the tibble above is a regular number
with a formatting method. It always will be shown with three digits
after the decimal point. This also applies to columns derived from
x.
library(dplyr)
tbl2 <-
tbl %>%
mutate(
y = x + 1,
z = x * x,
v = y + z,
lag = lag(x, default = x[[1]]),
sin = sin(x),
mean = mean(v),
var = var(x)
)
tbl2Summaries also maintain the formatting.
Same for pivoting operations.
library(tidyr)
stocks <-
expand_grid(id = factor(1:4), year = 2018:2022) %>%
mutate(stock = currency(runif(20) * 10000))
stocks %>%
pivot_wider(id_cols = id, names_from = year, values_from = stock)For ggplot2 we need to do some work to show apply the formatting to the scales.
library(ggplot2)
# Needs https://github.com/tidyverse/ggplot2/pull/4065 or similar
stocks %>%
ggplot(aes(x = year, y = stock, color = id)) +
geom_line()
#> Error: object 'stocks' not foundIt pays off to specify formatting very early in the process. The diagram below shows the principal stages of data analysis and exploration from “R for data science”.
The subsequent diagram adds data formats, communication options, and explicit data formatting. The original r4ds transitions are highlighted in bold. There are two principal options where to apply formatting for results: right before communicating them, or right after importing.
Applying formatting early in the process gives the added benefit of showing the data in a useful format during the “Tidy”, “Transform”, and “Visualize” stages. For this to be useful, we need to ensure that the formatting options applied early:
Ensuring stickiness is difficult, and is insufficient for a dbplyr workflow where parts of the “Tidy”, “Transform” or even “Visualize” stages are run on the database. Often it’s possible to derive a rule-based approach for formatting.
tbl3 <-
tibble(id = letters[1:3], x = 9:11) %>%
mutate(
y = x + 1,
z = x * x,
v = y + z,
lag = lag(x, default = x[[1]]),
sin = sin(x),
mean = mean(v),
var = var(x)
)
tbl3
tbl3 %>%
mutate(
across(where(is.numeric), ~ digits(.x, 3)),
across(where(~ is.numeric(.x) && mean(.x) > 50), ~ digits(.x, 1))
)These rules can be stored in quos():
rules <- quos(
across(where(is.numeric), ~ digits(.x, 3)),
across(where(~ is.numeric(.x) && mean(.x) > 50), ~ digits(.x, 1))
)
tbl3 %>%
mutate(!!!rules)This poses a few drawbacks:
mutate() works, and are executed multiple
timesWhat would a good API for rule-based formatting look like?