Debian Codegraph @

R/vctrs.R

raw
1 #' Functions for working with the vctrs package
2 #'
3 #' These make it clear that we need to use the tibble behavior when joining,
4 #' concatenating or casting `skim_df` objects. For a better discussion, on
5 #' why this is important and how these functions work, see:
6 #' <https://vctrs.r-lib.org/reference/howto-faq-coercion-data-frame.html>.
7 #'
8 #' `vec_ptype2.*` handles finding common prototypes between `skim_df` and
9 #' similar objects. `vec_cast.*` handles casting between objects. Note that
10 #' as of `dplyr 1.0.2`, [dplyr::bind_rows()] does not full support combining
11 #' attributes and [vctrs::vec_rbind()] is preferred instead.
12 #'
13 #' @importFrom vctrs vec_ptype2 vec_cast
14 #' @name skimr-vctrs
15 #' @keywords internal
16 NULL
17
18 #' @rdname skimr-vctrs
19 #' @export
20 vec_ptype2.skim_df.skim_df <- function(x, y, ...) {
21 combine_compatible_objects(x, y, ...)
22 }
23
24 #' @rdname skimr-vctrs
25 #' @export
26 vec_ptype2.skim_df.tbl_df <- function(x, y, ...) {
27 combine_compatible_objects(x, y, ...)
28 }
29
30 #' @rdname skimr-vctrs
31 #' @export
32 vec_ptype2.tbl_df.skim_df <- function(x, y, ...) {
33 combine_compatible_objects(x, y, ...)
34 }
35
36 #' @rdname skimr-vctrs
37 #' @export
38 vec_cast.skim_df.skim_df <- function(x, to, ...) {
39 cast_compatible_object(x, to, ...)
40 }
41
42 #' @rdname skimr-vctrs
43 #' @export
44 vec_cast.skim_df.tbl_df <- function(x, to, ...) {
45 vctrs::tib_cast(x, to, ...)
46 }
47
48 #' @rdname skimr-vctrs
49 #' @export
50 vec_cast.tbl_df.skim_df <- function(x, to, ...) {
51 vctrs::tib_cast(x, to, ...)
52 }
53
54 #' We only combine skim_df's that were built with the same set of skimmers.
55 #' @noRd
56 has_compatible_skimmers <- function(x, y) {
57 has_identical_base(x, y) && has_identical_skimmers(x, y)
58 }
59
60 has_identical_base <- function(x, y) {
61 base_x <- attr(x, "base_skimmers") %||% attr(y, "base_skimmers")
62 base_y <- attr(y, "base_skimmers") %||% attr(x, "base_skimmers")
63 identical(base_x, base_y)
64 }
65
66 has_identical_skimmers <- function(x, y) {
67 skim_list_x <- attr(x, "skimmers_used") %||% attr(y, "skimmers_used")
68 skim_list_y <- attr(y, "skimmers_used") %||% attr(x, "skimmers_used")
69 x_names <- names(skim_list_x)
70 y_names <- names(skim_list_y)
71 all_names <- union(x_names, y_names)
72 all(purrr::map_lgl(
73 all_names,
74 check_identical_skimmers,
75 x_names,
76 y_names,
77 skim_list_x,
78 skim_list_y
79 ))
80 }
81
82 check_identical_skimmers <- function(name,
83 x_names,
84 y_names,
85 skim_list_x,
86 skim_list_y) {
87 if ((name %in% x_names) && (name %in% y_names)) {
88 identical(skim_list_x[name], skim_list_y[name])
89 } else {
90 TRUE
91 }
92 }
93
94 combine_compatible_objects <- function(x, y, ..., x_arg = "", y_arg = "") {
95 if (!has_compatible_skimmers(x, y)) {
96 vctrs::stop_incompatible_type(
97 x,
98 y,
99 x_arg = x_arg,
100 y_arg = y_arg,
101 details = "Can't combine different sets of skim functions."
102 )
103 }
104
105 if (could_be_skim_df(x) && could_be_skim_df(y)) {
106 reassign_skim_attrs(
107 vctrs::tib_ptype2(x, y, ...),
108 x,
109 data_rows = data_rows(x) + data_rows(y),
110 data_cols = data_cols(x) + data_cols(y),
111 df_name = paste0(df_name(x), "+", df_name(y)),
112 groups = c(group_names(x), group_names(y)),
113 skimmers_used = union(skimmers_used(x), skimmers_used(y))
114 )
115 } else {
116 strip_skim_attrs(vctrs::tib_ptype2(x, y, ...))
117 }
118 }
119
120 cast_compatible_object <- function(x, to, ..., x_arg = "", to_arg = "") {
121 out <- vctrs::tib_cast(x, to, ..., x_arg = x_arg, to_arg = to_arg)
122 if (could_be_skim_df(out)) {
123 reassign_skim_attrs(out, to)
124 } else {
125 strip_skim_attrs(out)
126 }
127 }