| 1 |
--- |
| 2 |
title: "Controlling display of numbers" |
| 3 |
output: html_vignette |
| 4 |
vignette: > |
| 5 |
%\VignetteIndexEntry{Controlling display of numbers} |
| 6 |
%\VignetteEngine{knitr::rmarkdown} |
| 7 |
%\VignetteEncoding{UTF-8} |
| 8 |
--- |
| 9 |
|
| 10 |
This content has moved to `vignette("numbers", package = "tibble")`. |
| 11 |
|
| 12 |
`````{asis echo = FALSE} |
| 13 |
|
| 14 |
## Integrating with other classes |
| 15 |
|
| 16 |
### gt |
| 17 |
|
| 18 |
- Consumption via new `fmt_auto()`? |
| 19 |
|
| 20 |
FIXME |
| 21 |
|
| 22 |
### units |
| 23 |
|
| 24 |
xxx{r numbers-17} |
| 25 |
library(units) |
| 26 |
|
| 27 |
set_units.pillar_num <- function(x, ...) { |
| 28 |
unclassed <- x |
| 29 |
class(unclassed) <- NULL |
| 30 |
set_units(unclassed, ...) |
| 31 |
} |
| 32 |
|
| 33 |
m <- set_units(1:3, m) |
| 34 |
km <- set_units(1:3, km) |
| 35 |
|
| 36 |
tibble( |
| 37 |
sci_int = set_num_opts(m + km, notation = "sci"), |
| 38 |
digits_int = set_num_opts(km + m, digits = 4), |
| 39 |
sci_ext = set_units(num(1:3 + 0.001, notation = "sci"), km) |
| 40 |
) |
| 41 |
|
| 42 |
tibble( |
| 43 |
sci_int = set_num_opts(m, notation = "sci") + km, |
| 44 |
digits_int = set_num_opts(km, digits = 4) + m, |
| 45 |
sci_ext = set_units(num(1:3, notation = "sci"), m) + km |
| 46 |
) |
| 47 |
xxx |
| 48 |
|
| 49 |
### formattable |
| 50 |
|
| 51 |
xxx{r numbers-18, error = TRUE} |
| 52 |
library(formattable) |
| 53 |
|
| 54 |
pillar_shaft.formattable <- function(x, ...) { |
| 55 |
pillar::new_pillar_shaft_simple(format(x), align = "right") |
| 56 |
} |
| 57 |
|
| 58 |
pillar_shaft.formattable_currency <- function(x, ...) { |
| 59 |
formattable <- attr(x, "formattable", exact = TRUE) |
| 60 |
|
| 61 |
pillar_shaft(num(unclass(x), digits = formattable$digits)) |
| 62 |
} |
| 63 |
|
| 64 |
pillar_shaft.formattable_percent <- function(x, ...) { |
| 65 |
formattable <- attr(x, "formattable", exact = TRUE) |
| 66 |
|
| 67 |
pillar_shaft(num(unclass(x), digits = formattable$digits, label = "%", scale = 100)) |
| 68 |
} |
| 69 |
|
| 70 |
pillar_shaft.formattable_scientific <- function(x, ...) { |
| 71 |
pillar_shaft(num(unclass(x), notation = "sci")) |
| 72 |
} |
| 73 |
|
| 74 |
type_sum.formattable <- function(x) { |
| 75 |
formattable <- attr(x, "formattable", exact = TRUE) |
| 76 |
|
| 77 |
if (inherits(x, "formattable_currency")) { |
| 78 |
I(sub("^formattable_", "", class(x)[[1]])) |
| 79 |
} else if (inherits(x, "formattable_percent")) { |
| 80 |
I("%") |
| 81 |
} else { |
| 82 |
abbreviate(sub("^formattable_", "", class(x)[[1]]), 4) |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
num_currency(1:3 * 100 + 0.1) |
| 87 |
num_percent(1:3 * 0.1 + 0.001) |
| 88 |
num_scientific(1:3 * 0.1 + 0.001) |
| 89 |
|
| 90 |
tibble( |
| 91 |
currency = num_currency(1:3 * 100 + 0.1), |
| 92 |
percent = num_percent(1:3 * 0.1 + 0.001), |
| 93 |
scientific = num_scientific(1:3 * 0.1 + 0.001) |
| 94 |
) |
| 95 |
xxx |
| 96 |
|
| 97 |
### scales |
| 98 |
|
| 99 |
xxx{r numbers-scales, error = TRUE} |
| 100 |
library(scales) |
| 101 |
|
| 102 |
x <- num(1:10 / 100, label = "%", scale = 100) |
| 103 |
|
| 104 |
scales::squish(x) |
| 105 |
|
| 106 |
x < 0 |
| 107 |
x < 0L |
| 108 |
|
| 109 |
scales::cscale(x, scales::rescale_pal()) |
| 110 |
xxx |
| 111 |
|
| 112 |
### ggplot2 |
| 113 |
|
| 114 |
xxx{r numbers-19} |
| 115 |
library(ggplot2) |
| 116 |
|
| 117 |
scale_type.pillar_num <- function(x, ...) { |
| 118 |
"continuous" |
| 119 |
} |
| 120 |
|
| 121 |
data.frame(x = x, y = 1:10) %>% |
| 122 |
ggplot(aes(x = x, y = y)) %>% |
| 123 |
+ geom_point() |
| 124 |
xxx |
| 125 |
|
| 126 |
## Rule-based decoration |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
xxx{r} |
| 131 |
library(dplyr) |
| 132 |
|
| 133 |
data_units <- |
| 134 |
palmerpenguins::penguins %>% |
| 135 |
mutate(across(ends_with("_mm"), set_units, "mm")) %>% |
| 136 |
mutate(across(ends_with("_g"), set_units, "g")) |
| 137 |
|
| 138 |
data_units %>% |
| 139 |
mutate(bill_area = bill_length_mm * bill_depth_mm, .after = island) |
| 140 |
xxx |
| 141 |
|
| 142 |
xxx{r eval = FALSE} |
| 143 |
data_decor <- |
| 144 |
data_units %>% |
| 145 |
decorate(year, digits = 0) %>% |
| 146 |
decorate(where(is.numeric), digits = 3) |
| 147 |
xxx |
| 148 |
|
| 149 |
xxx{r eval = FALSE} |
| 150 |
data_decor %>% |
| 151 |
mutate(bill_area = bill_length_mm * bill_depth_mm, .after = island) |
| 152 |
xxx |
| 153 |
|
| 154 |
xxx{r echo = FALSE} |
| 155 |
data_units %>% |
| 156 |
mutate(bill_area = bill_length_mm * bill_depth_mm, .after = island) %>% |
| 157 |
mutate(across(year, set_num_opts, digits = 0)) %>% |
| 158 |
mutate(across(where(is.numeric), set_num_opts, digits = 3)) |
| 159 |
xxx |
| 160 |
|
| 161 |
````` |