Debian Codegraph @

R/skim_obj.R

raw
1 #' Functions for accessing skim_df attributes
2 #'
3 #' These functions simplify access to attributes contained within a `skim_df`.
4 #' While all attributes are read-only, being able to extract this information
5 #' is useful for different analyses. These functions should always be preferred
6 #' over calling base R's attribute functions.
7 #'
8 #' @param object A `skim_df` or `skim_list`.
9 #' @return Data contained within the requested `skimr` attribute.
10 #' @name skim-attr
11 NULL
12
13 #' @describeIn skim-attr Get the number of rows in the skimmed data frame.
14 #' @export
15 data_rows <- function(object) {
16 attr(object, "data_rows")
17 }
18
19 #' @describeIn skim-attr Get the number of columns in the skimmed data frame.
20 #' @export
21 data_cols <- function(object) {
22 attr(object, "data_cols")
23 }
24
25 #' @describeIn skim-attr Get the name of the skimmed data frame. This is only
26 #' available in contexts where the name can be looked up. This is often not
27 #' the case within a pipeline.
28 #' @export
29 df_name <- function(object) {
30 attr(object, "df_name")
31 }
32
33 #' @describeIn skim-attr Get the key of the skimmed data.table. This is only
34 #' available in contexts where `data` is of class `data.table`.
35 #' @export
36 dt_key <- function(object) {
37 attr(object, "dt_key")
38 }
39
40 #' @describeIn skim-attr Get the names of the groups in the original data frame.
41 #' Only available if the data was grouped. Otherwise, `NULL`.
42 #' @export
43 group_names <- function(object) {
44 attr(object, "groups")
45 }
46
47 #' @describeIn skim-attr Get the names of the base skimming functions used.
48 #' @export
49 base_skimmers <- function(object) {
50 attr(object, "base_skimmers")
51 }
52
53 #' @describeIn skim-attr Get the names of the skimming functions used, separated
54 #' by data type.
55 #' @export
56 skimmers_used <- function(object) {
57 attr(object, "skimmers_used")
58 }
59
60
61 #' Test if an object is compatible with `skimr`
62 #'
63 #' Objects within `skimr` are identified by a class, but they require additional
64 #' attributes and data columns for all operations to succeed. These checks help
65 #' ensure this. While they have some application externally, they are mostly
66 #' used internally.
67 #'
68 #' Most notably, a `skim_df` has columns `skim_type` and `skim_variable`. And
69 #' has the following special attributes
70 #'
71 #' * `data_rows`: n rows in the original data
72 #' * `data_cols`: original number of columns
73 #' * `df_name`: name of the original data frame
74 #' * `dt_key`: name of the key if original is a data.table
75 #' * `groups`: if there were group variables
76 #' * `base_skimmers`: names of functions applied to all skim types
77 #' * `skimmers_used`: names of functions used to skim each type
78 #'
79 #' The functions in these checks work like [all.equal()]. The return `TRUE` if
80 #' the check passes, or otherwise notifies why the check failed. This makes them
81 #' more useful when throwing errors.
82 #'
83 #' @param object Any `R` object.
84 #' @name skim-obj
85 NULL
86
87 #' @describeIn skim-obj Does the object have the `skim_type` column?
88 #' @export
89 has_type_column <- function(object) {
90 make_issue("skim_type" %in% names(object), "missing column `skim_type`")
91 }
92
93 #' @describeIn skim-obj Does the object have the `skim_variable` column?
94 #' @export
95 has_variable_column <- function(object) {
96 make_issue(
97 "skim_variable" %in% names(object), "missing column `skim_variable`"
98 )
99 }
100
101 #' @describeIn skim-obj Does the object have the appropriate `skimr` attributes?
102 #' @export
103 has_skimr_attributes <- function(object) {
104 skimr_attrs <- c(
105 "data_rows", "data_cols", "df_name", "dt_key", "base_skimmers",
106 "skimmers_used"
107 )
108 missing <- !(skimr_attrs %in% names(attributes(object)))
109 make_issue(!any(missing), show_missing_attributes(skimr_attrs, missing))
110 }
111
112 show_missing_attributes <- function(attributes, missing) {
113 missing_msg <- if (length(missing) > 0) {
114 paste0(attributes[missing], collapse = ", ")
115 } else {
116 ""
117 }
118 paste("missing attributes:", missing_msg)
119 }
120
121 #' @describeIn skim-obj Does the object have a `skim_type` attribute? This makes
122 #' it a `one_skim_df`.
123 #' @export
124 has_skim_type_attribute <- function(object) {
125 make_issue(
126 "skim_type" %in% names(attributes(object)),
127 "missing attribute: `skim_type`"
128 )
129 }
130
131 #' @describeIn skim-obj Does the object have skimmers?
132 #' @export
133 has_skimmers <- function(object) {
134 base <- base_skimmers(object)
135 skimmers <- reconcile_skimmers(
136 object,
137 group_names(object),
138 base
139 )
140 make_issue(
141 any(lengths(skimmers) > 0, base %in% names(object)),
142 "does not have defined skimmers."
143 )
144 }
145
146 #' @describeIn skim-obj Is the object a data frame?
147 #' @export
148 is_data_frame <- function(object) {
149 make_issue(inherits(object, "data.frame"), "not a data.frame")
150 }
151
152 make_issue <- function(check, message) {
153 structure(check, message = if (!check) message else character())
154 }
155
156 #' @describeIn skim-obj Is the object a `skim_df`?
157 #' @export
158 is_skim_df <- function(object) {
159 check_issues(
160 "Object is not a `skim_df`",
161 is_data_frame(object),
162 has_type_column(object),
163 has_variable_column(object),
164 has_skimr_attributes(object),
165 has_skimmers(object)
166 )
167 }
168
169 #' @describeIn skim-obj Is the object a `one_skim_df`? This is similar to a
170 #' `skim_df`, but does not have the `type` column. That is stored as an
171 #' attribute instead.
172 #' @export
173 is_one_skim_df <- function(object) {
174 check_issues(
175 "Object is not a `one_skim_df`",
176 is_data_frame(object),
177 has_skim_type_attribute(object),
178 has_variable_column(object),
179 has_skimr_attributes(object)
180 )
181 }
182
183 #' @describeIn skim-obj Is the object a `skim_list`?
184 #' @export
185 is_skim_list <- function(object) {
186 check_issues(
187 "Object is not a `skim_list`",
188 has_skimr_attributes(object),
189 !!!purrr::map(object, is_one_skim_df)
190 )
191 }
192
193 #' @describeIn skim-obj Is this a data frame with `skim_variable` and
194 #' `skim_type` columns?
195 #' @export
196 could_be_skim_df <- function(object) {
197 check_issues(
198 "Object cannot behave like a `skim_df`",
199 is_data_frame(object),
200 has_variable_column(object),
201 has_type_column(object)
202 )
203 }
204
205 check_issues <- function(condition, ...) {
206 issues <- rlang::list2(...)
207 msgs <- purrr::map(issues, ~ attr(.x, "message"))
208 check <- all(purrr::flatten_lgl(issues))
209 message <- if (check) {
210 character()
211 } else {
212 paste0(condition, ": ", paste0(unlist(msgs), collapse = "; "))
213 }
214 structure(check, message = message)
215 }
216
217 #' @describeIn skim-obj Stop if the object is not a `skim_df`.
218 #' @export
219 assert_is_skim_df <- function(object) {
220 assert(is_skim_df(object))
221 }
222
223 #' @describeIn skim-obj Stop if the object is not a `skim_list`.
224 #' @export
225 assert_is_skim_list <- function(object) {
226 assert(is_skim_list(object))
227 }
228
229 #' @describeIn skim-obj Stop if the object is not a `one_skim_df`.
230 #' @export
231 assert_is_one_skim_df <- function(object) {
232 assert(is_one_skim_df(object))
233 }
234
235 assert <- function(check) {
236 if (check) {
237 invisible(NULL)
238 } else {
239 stop(attr(check, "message"))
240 }
241 }
242
243 #' Remove `skimr` class if not needed.
244 #' @noRd
245 strip_skim_attrs <- function(object) {
246 attrs <- attributes(object)
247 stripped <- purrr::list_modify(
248 attrs,
249 class = class(object)[-1],
250 data_rows = NULL,
251 data_cols = NULL,
252 df_name = NULL,
253 dt_key = NULL,
254 base_skimmers = NULL,
255 skimmers_used = NULL
256 )
257 attributes(object) <- stripped
258 object
259 }
260
261 #' Pass attributes from a `skimr` object to a new object.
262 #' @noRd
263 reassign_skim_attrs <- function(object, skim_df, ...) {
264 defaults <- list(
265 class = c("skim_df", "tbl_df", "tbl", "data.frame"),
266 data_rows = data_rows(skim_df),
267 data_cols = data_cols(skim_df),
268 df_name = df_name(skim_df),
269 dt_key = dt_key(skim_df),
270 groups = group_names(skim_df),
271 base_skimmers = base_skimmers(skim_df),
272 skimmers_used = skimmers_used(skim_df)
273 )
274 updated <- purrr::list_modify(defaults, ...)
275 assign_new_attributes(object, !!!updated)
276 }
277
278 assign_new_attributes <- function(object, ...) {
279 original <- attributes(object)
280 attributes(object) <- purrr::list_modify(original, ...)
281 object
282 }