Debian Codegraph @

R/tidyverse.R

raw
1 type_sum.units <- function(x, ...) {
2 gr = units_options("group")
3 # see https://github.com/r-lib/pillar/issues/73 : currently the [ and ] mess up things.
4 structure(paste0(gr[1], as.character(units(x)), gr[2]),
5 class = "type_sum_units")
6 }
7
8 type_sum.mixed_units <- function(x, ...) {
9 "mixed_units"
10 }
11
12 pillar_shaft.units <- function(x, ...) {
13 pillar::pillar_shaft(unclass(x), ...)
14 }
15
16 pillar_shaft.mixed_units <- function(x, ...) {
17 u_char <- pillar::style_subtle(sapply(x, function(unit) format(unit[0])))
18 out <- paste0(as.numeric(x), u_char)
19 pillar::new_pillar_shaft_simple(out, ..., align = "right", min_width = 6)
20 }
21
22 format_type_sum.type_sum_units <- function(x, width, ...) {
23 pillar::style_subtle(x)
24 }
25
26
27 # vctrs proxying and restoration -------------------------------------
28
29 vec_proxy.units = function(x, ...) {
30 x
31 }
32 vec_restore.units = function(x, to, ...) {
33 restore_units(x, to)
34 }
35 restore_units <- function(x, to) {
36 out <- .as.units(x, units(to))
37 attr(out, "pillar") <- attr(to, "pillar")
38 out
39 }
40
41
42 # vctrs coercion -----------------------------------------------------
43
44 vec_ptype2.units.units = function(x, y, ..., x_arg = "", y_arg = "") {
45 x_units = units(x)
46 y_units = units(y)
47
48 if (!ud_are_convertible(x_units, y_units))
49 vctrs::stop_incompatible_type(x, y, x_arg = x_arg, y_arg = y_arg)
50
51 x_bare = drop_units(x)
52 y_bare = drop_units(y)
53 common = vctrs::vec_ptype2(x_bare, y_bare, ..., x_arg = x_arg, y_arg = y_arg)
54
55 # Use left-hand side units
56 set_units(common, x_units, mode = "standard")
57 }
58
59 vec_cast.units.units = function(x, to, ..., x_arg = "", to_arg = "") {
60 x_units = units(x)
61 to_units = units(to)
62
63 if (!ud_are_convertible(x_units, to_units))
64 vctrs::stop_incompatible_cast(x, to, x_arg = x_arg, to_arg = to_arg)
65
66 # Convert to target units before converting base type. Unit
67 # conversion might change the type and so must happen first.
68 out = set_units(x, to_units, mode = "standard")
69
70 out_bare = drop_units(out)
71 to_bare = drop_units(to)
72 out = vctrs::vec_cast(out_bare, to_bare, ..., x_arg = x_arg, to_arg = to_arg)
73
74 # Set target units again
75 set_units(out, to_units, mode = "standard")
76 }
77
78
79 #nocov start
80 register_all_s3_methods <- function() {
81 register_s3_method("pillar::type_sum", "units")
82 register_s3_method("pillar::type_sum", "mixed_units")
83 register_s3_method("pillar::pillar_shaft", "units")
84 register_s3_method("pillar::pillar_shaft", "mixed_units")
85 register_s3_method("pillar::format_type_sum", "type_sum_units")
86 register_s3_method("vctrs::vec_proxy", "units")
87 register_s3_method("vctrs::vec_restore", "units")
88 register_s3_method("vctrs::vec_ptype2", "units.units")
89 register_s3_method("vctrs::vec_cast", "units.units")
90 register_s3_method("ggplot2::scale_type", "units")
91 register_s3_method("ggplot2::limits", "units")
92 }
93
94 register_s3_method <- function(generic, class, fun=NULL) {
95 stopifnot(is.character(generic), length(generic) == 1)
96 stopifnot(is.character(class), length(class) == 1)
97
98 pieces <- strsplit(generic, "::")[[1]]
99 stopifnot(length(pieces) == 2)
100 package <- pieces[[1]]
101 generic <- pieces[[2]]
102
103 if (is.null(fun))
104 fun <- get(paste0(generic, ".", class), envir=parent.frame())
105 stopifnot(is.function(fun))
106
107 if (package %in% loadedNamespaces())
108 registerS3method(generic, class, fun, envir=asNamespace(package))
109
110 # Always register hook in case package is later unloaded & reloaded
111 setHook(packageEvent(package, "onLoad"), function(...)
112 registerS3method(generic, class, fun, envir=asNamespace(package)))
113 }
114 # nocov end