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