Debian Codegraph @

tests/testthat/test-skim.R

raw
1 test_that("skim returns expected response for numeric vectors", {
2 input <- skim(mtcars, mpg)
3
4 # dimensions
5 expect_n_rows(input, 1)
6 expect_n_columns(input, 12)
7
8 # classes
9 expect_s3_class(input, c("skim_df", "tbl_df", "tbl", "data.frame"))
10 expect_named(input, c(
11 "skim_type", "skim_variable", "n_missing", "complete_rate",
12 "numeric.mean", "numeric.sd", "numeric.p0", "numeric.p25",
13 "numeric.p50", "numeric.p75", "numeric.p100", "numeric.hist"
14 ))
15
16 # attributes
17 attrs <- attributes(input)
18 expect_equal(attrs$data_rows, 32)
19 expect_equal(attrs$data_cols, 11)
20 expect_equal(attrs$df_name, "`mtcars`")
21 expect_equal(
22 attrs$skimmers_used,
23 list(
24 numeric = c("mean", "sd", "p0", "p25", "p50", "p75", "p100", "hist")
25 )
26 )
27
28 # values
29 expect_identical(unname(input$skim_variable), "mpg")
30 expect_identical(input$skim_type, "numeric")
31 expect_identical(input$n_missing, 0L)
32 expect_equal(input$complete_rate, 1)
33 expect_equal(input$numeric.mean, 20.1, tolerance = 0.1)
34 expect_equal(input$numeric.p0, 10.4, tolerance = 0.1)
35 expect_equal(input$numeric.p25, 15.4, tolerance = 0.1)
36 expect_equal(input$numeric.p50, 19.2, tolerance = 0.1)
37 expect_equal(input$numeric.p75, 22.8, tolerance = 0.1)
38 expect_equal(input$numeric.p100, 33.9, tolerance = 0.1)
39 skip_if_not(l10n_info()$`UTF-8`)
40 expect_identical(input$numeric.hist, "▃▇▅▁▂")
41 })
42
43 test_that("skim handles numeric vectors with NAs and extreme numbers", {
44 patho <- tibble::tibble(
45 vals = c((2^.Machine$double.digits), NA, -(2^.Machine$double.digits))
46 )
47
48 input <- skim(patho)
49
50 # attributes
51 attrs <- attributes(input)
52 expect_equal(attrs$data_rows, 3)
53 expect_equal(attrs$data_cols, 1)
54 expect_equal(attrs$df_name, "`patho`")
55
56 # values
57 expect_identical(input$skim_variable, "vals")
58 expect_identical(input$skim_type, "numeric")
59 expect_identical(input$n_missing, 1L)
60 expect_equal(input$complete_rate, .667, tolerance = .001)
61 expect_equal(input$numeric.mean, 0)
62 expect_equal(input$numeric.sd, 1.27e16, tolerance = 1e14)
63 expect_equal(input$numeric.p0, -9.01e+15, tolerance = 1e14)
64 expect_equal(input$numeric.p25, -4.5e+15, tolerance = 1e14)
65 expect_equal(input$numeric.p50, 0)
66 expect_equal(input$numeric.p75, 4.5e+15, tolerance = 1e14)
67 expect_equal(input$numeric.p100, 9.01e+15, tolerance = 1e14)
68 skip_if_not(l10n_info()$`UTF-8`)
69 expect_identical(input$numeric.hist, "▇▁▁▁▇")
70 })
71
72 test_that("numeric skim is calculated correctly when x is all NAs.", {
73 nas <- as.numeric(c(NA, NA, NA))
74 x <- tibble::tibble(nas)
75 input <- skim(x)
76
77 # attributes
78 attrs <- attributes(input)
79 expect_equal(attrs$data_rows, 3)
80 expect_equal(attrs$data_cols, 1)
81 expect_equal(attrs$df_name, "`x`")
82
83 # values
84 expect_identical(input$skim_variable, "nas")
85 expect_identical(input$skim_type, "numeric")
86 expect_identical(input$n_missing, 3L)
87 expect_identical(input$complete_rate, 0)
88 expect_equal(input$numeric.mean, NaN)
89 expect_equal(input$numeric.sd, NaN)
90 expect_NA(input$numeric.p0)
91 expect_NA(input$numeric.p25)
92 expect_NA(input$numeric.p50)
93 expect_NA(input$numeric.p75)
94 expect_NA(input$numeric.p100)
95 skip_if_not(l10n_info()$`UTF-8`)
96 expect_identical(input$numeric.hist, " ")
97 })
98
99 test_that("numeric skim is calculated correctly when x is all zeores or NAs.", {
100 na_and_zeros <- as.numeric(c(NA, NA, NA, 0))
101 x <- tibble::tibble(na_and_zeros)
102 input <- skim(x)
103
104 # attributes
105 attrs <- attributes(input)
106 expect_equal(attrs$data_rows, 4)
107 expect_equal(attrs$data_cols, 1)
108 expect_equal(attrs$df_name, "`x`")
109
110 # values
111 expect_identical(input$skim_variable, "na_and_zeros")
112 expect_identical(input$skim_type, "numeric")
113 expect_identical(input$n_missing, 3L)
114 expect_equal(input$complete_rate, .25)
115 expect_equal(input$numeric.mean, 0)
116 expect_NA(input$numeric.sd)
117 expect_equal(input$numeric.p0, 0)
118 expect_equal(input$numeric.p25, 0)
119 expect_equal(input$numeric.p50, 0)
120 expect_equal(input$numeric.p75, 0)
121 expect_equal(input$numeric.p100, 0)
122 skip_if_not(l10n_info()$`UTF-8`)
123 expect_identical(input$numeric.hist, "▁▁▇▁▁")
124 })
125
126 test_that("Skimming with non-finite values works", {
127 inf_vals <- c(Inf, 0, -Inf)
128 x <- tibble::tibble(inf_vals)
129 expect_warning(input <- skim(x))
130
131 expect_identical(input$skim_variable, "inf_vals")
132 expect_identical(input$skim_type, "numeric")
133 expect_identical(input$n_missing, 0L)
134 expect_equal(input$complete_rate, 1)
135 expect_equal(input$numeric.mean, NaN)
136 expect_equal(input$numeric.sd, NaN)
137 expect_equal(input$numeric.p0, -Inf)
138 expect_equal(input$numeric.p25, -Inf)
139 expect_equal(input$numeric.p50, 0)
140 expect_equal(input$numeric.p75, Inf)
141 expect_equal(input$numeric.p100, Inf)
142 skip_if_not(l10n_info()$`UTF-8`)
143 expect_identical(input$numeric.hist, "▁▁▇▁▁")
144 })
145
146 test_that("skim returns expected response for factor vectors", {
147 input <- skim(iris, Species)
148
149 # dimensions
150 expect_n_rows(input, 1)
151 expect_n_columns(input, 7)
152 expect_named(input, c(
153 "skim_type", "skim_variable", "n_missing", "complete_rate",
154 "factor.ordered", "factor.n_unique", "factor.top_counts"
155 ))
156
157 # attributes
158 attrs <- attributes(input)
159 expect_equal(attrs$data_rows, 150)
160 expect_equal(attrs$data_cols, 5)
161 expect_equal(attrs$df_name, "`iris`")
162 expect_equal(
163 attrs$skimmers_used,
164 list(factor = c("ordered", "n_unique", "top_counts"))
165 )
166
167 # values
168 expect_identical(unname(input$skim_variable), "Species")
169 expect_identical(input$skim_type, "factor")
170 expect_identical(input$n_missing, 0L)
171 expect_equal(input$complete_rate, 1)
172 expect_false(input$factor.ordered)
173 expect_identical(input$factor.top_counts, "set: 50, ver: 50, vir: 50")
174 })
175
176 test_that("skim handles factors when NAs are present", {
177 dat <- iris
178 dat$Species[15:18] <- NA
179 input <- skim(dat, Species)
180 expect_identical(input$n_missing, 4L)
181 expect_equal(input$complete_rate, .973, tolerance = .001)
182 expect_identical(input$factor.top_counts, "ver: 50, vir: 50, set: 46")
183 })
184
185
186 test_that("skim returns expected response for character vectors", {
187 dat <- c("AAAB", "ABc", "acb", NA, "", " ")
188 x <- tibble::tibble(dat)
189 input <- skim(x)
190
191 # dimensions
192 expect_n_rows(input, 1)
193 expect_n_columns(input, 9)
194
195 # classes
196 expect_s3_class(input, c("skim_df", "tbl_df", "tbl", "data.frame"))
197 expect_named(input, c(
198 "skim_type", "skim_variable", "n_missing", "complete_rate",
199 "character.min", "character.max", "character.empty",
200 "character.n_unique", "character.whitespace"
201 ))
202
203 # attributes
204 attrs <- attributes(input)
205 expect_equal(attrs$data_rows, 6)
206 expect_equal(attrs$data_cols, 1)
207 expect_equal(attrs$df_name, "`x`")
208 expect_equal(
209 attrs$skimmers_used,
210 list(character = c("min", "max", "empty", "n_unique", "whitespace"))
211 )
212
213 # values
214 expect_identical(input$skim_variable, "dat")
215 expect_identical(input$skim_type, "character")
216 expect_identical(input$n_missing, 1L)
217 expect_equal(input$complete_rate, .833, tolerance = .001)
218 expect_identical(input$character.min, 0L)
219 expect_identical(input$character.max, 4L)
220 expect_identical(input$character.empty, 1L)
221 expect_identical(input$character.n_unique, 5L)
222 expect_identical(input$character.whitespace, 1L)
223 })
224
225 test_that("skim returns expected response for logical vectors", {
226 dat <- dplyr::mutate(chickwts, log_col = stringr::str_detect(feed, "ea"))
227 input <- skim(dat, log_col)
228
229 # dimensions
230 expect_n_rows(input, 1)
231 expect_n_columns(input, 6)
232
233 # classes
234 expect_s3_class(input, c("skim_df", "tbl_df", "tbl", "data.frame"))
235 expect_named(input, c(
236 "skim_type", "skim_variable", "n_missing", "complete_rate",
237 "logical.mean", "logical.count"
238 ))
239
240 # attributes
241 attrs <- attributes(input)
242 expect_equal(attrs$data_rows, 71)
243 expect_equal(attrs$data_cols, 3)
244 expect_equal(attrs$df_name, "`dat`")
245 expect_equal(
246 attrs$skimmers_used,
247 list(logical = c("mean", "count"))
248 )
249
250 # values
251 expect_identical(unname(input$skim_variable), "log_col")
252 expect_identical(input$skim_type, "logical")
253 expect_identical(input$n_missing, 0L)
254 expect_equal(input$complete_rate, 1)
255 expect_equal(input$logical.mean, 0.49, tolerance = .01)
256 expect_identical(input$logical.count, "FAL: 36, TRU: 35")
257 })
258
259 test_that("skim returns expected response for logical vectors with NA values", {
260 dat <- dplyr::mutate(chickwts, log_col = stringr::str_detect(feed, "ea"))
261 dat$log_col[15:18] <- NA
262 input <- skim(dat, log_col)
263
264 expect_identical(input$n_missing, 4L)
265 expect_equal(input$complete_rate, .944, tolerance = .001)
266 expect_equal(input$logical.mean, 0.52, tolerance = .01)
267 expect_identical(input$logical.count, "TRU: 35, FAL: 32")
268 })
269
270 test_that("skim returns expected response for complex vectors", {
271 dat <- dplyr::mutate(chickwts, test_complex = weight)
272 dat$test_complex[1:2] <- dat$test_complex[1:2] + 2i
273 dat$test_complex[15:18] <- NA_complex_
274 input <- skim(dat, test_complex)
275
276 # dimensions
277 expect_n_rows(input, 1)
278 expect_n_columns(input, 5)
279
280 # classes
281 expect_s3_class(input, c("skim_df", "tbl_df", "tbl", "data.frame"))
282 expect_named(
283 input,
284 c(
285 "skim_type", "skim_variable", "n_missing", "complete_rate", "complex.mean"
286 )
287 )
288
289 # attributes
290 attrs <- attributes(input)
291 expect_equal(attrs$data_rows, 71)
292 expect_equal(attrs$data_cols, 3)
293 expect_equal(attrs$df_name, "`dat`")
294 expect_equal(
295 attrs$skimmers_used,
296 list(complex = "mean")
297 )
298
299 # values
300 expect_identical(unname(input$skim_variable), "test_complex")
301 expect_identical(input$skim_type, "complex")
302 expect_identical(input$n_missing, 4L)
303 expect_equal(input$complete_rate, .944, tolerance = .001)
304 expect_equal(input$complex.mean, 265.2687 + 0.0597i, tolerance = .001)
305 })
306
307 test_that("skim returns expected response for Date vectors", {
308 dat <- seq(as.Date("2011-07-01"), by = 1, length.out = 9)
309 x <- tibble::tibble(dat)
310 input <- skim(x)
311
312 # dimensions
313 expect_n_rows(input, 1)
314 expect_n_columns(input, 8)
315
316 # classes
317 expect_s3_class(input, c("skim_df", "tbl_df", "tbl", "data.frame"))
318 expect_named(input, c(
319 "skim_type", "skim_variable", "n_missing", "complete_rate",
320 "Date.min", "Date.max", "Date.median", "Date.n_unique"
321 ))
322
323 # attributes
324 attrs <- attributes(input)
325 expect_equal(attrs$data_rows, 9)
326 expect_equal(attrs$data_cols, 1)
327 expect_equal(attrs$df_name, "`x`")
328 expect_equal(
329 attrs$skimmers_used,
330 list(Date = c("min", "max", "median", "n_unique"))
331 )
332
333 # values
334 expect_identical(input$skim_variable, "dat")
335 expect_identical(input$skim_type, "Date")
336 expect_identical(input$n_missing, 0L)
337 expect_equal(input$complete_rate, 1)
338 expect_equal(input$Date.min, as.Date("2011-07-01"))
339 expect_equal(input$Date.max, as.Date("2011-07-09"))
340 expect_equal(input$Date.median, as.Date("2011-07-05"))
341 expect_identical(input$Date.n_unique, 9L)
342 })
343
344 test_that("skim returns expected response for ts vectors", {
345 input <- skim(freeny, y)
346
347 # dimensions
348 expect_n_rows(input, 1)
349 expect_n_columns(input, 14)
350
351 # classes
352 expect_s3_class(input, c("skim_df", "tbl_df", "tbl", "data.frame"))
353 expect_named(input, c(
354 "skim_type", "skim_variable", "n_missing", "complete_rate",
355 "ts.start", "ts.end", "ts.frequency", "ts.deltat", "ts.mean", "ts.sd",
356 "ts.min", "ts.max", "ts.median", "ts.line_graph"
357 ))
358
359 # attributes
360 attrs <- attributes(input)
361 expect_equal(attrs$data_rows, 39)
362 expect_equal(attrs$data_cols, 5)
363 expect_equal(attrs$df_name, "`freeny`")
364 expect_equal(
365 attrs$skimmers_used,
366 list(ts = c(
367 "start", "end", "frequency", "deltat", "mean", "sd", "min",
368 "max", "median", "line_graph"
369 ))
370 )
371
372 # values
373 expect_identical(unname(input$skim_variable), "y")
374 expect_identical(input$skim_type, "ts")
375 expect_identical(input$n_missing, 0L)
376 expect_equal(input$complete_rate, 1)
377 expect_equal(input$ts.start, 1962)
378 expect_equal(input$ts.end, 1971)
379 expect_equal(input$ts.frequency, 4)
380 expect_equal(input$ts.deltat, 0.25)
381 expect_equal(input$ts.mean, 9.31, tolerance = 0.001)
382 expect_equal(input$ts.sd, 0.316, tolerance = 0.01)
383 expect_equal(input$ts.min, 8.79, tolerance = 0.001)
384 expect_equal(input$ts.max, 9.79, tolerance = 0.001)
385 expect_equal(input$ts.median, 9.31, tolerance = 0.001)
386 expect_identical(input$ts.line_graph, "⣀⣀⠤⠤⠒⠒⠉⠉")
387 })
388
389 test_that("skim returns expected response for POSIXct vectors", {
390 dat <- seq(as.POSIXct("2011-07-01 00:00:00", tz = "UTC"), by = 1, length.out = 10)
391 dat[2] <- NA
392 posix <- tibble::tibble(dat)
393 input <- skim(posix)
394
395 # dimensions
396 expect_n_rows(input, 1)
397 expect_n_columns(input, 8)
398
399 # classes
400 expect_s3_class(input, c("skim_df", "tbl_df", "tbl", "data.frame"))
401 expect_named(input, c(
402 "skim_type", "skim_variable", "n_missing", "complete_rate",
403 "POSIXct.min", "POSIXct.max", "POSIXct.median",
404 "POSIXct.n_unique"
405 ))
406
407 # attributes
408 attrs <- attributes(input)
409 expect_equal(attrs$data_rows, 10)
410 expect_equal(attrs$data_cols, 1)
411 expect_equal(attrs$df_name, "`posix`")
412 expect_equal(
413 attrs$skimmers_used,
414 list(POSIXct = c("min", "max", "median", "n_unique"))
415 )
416
417 # values
418 expect_identical(input$skim_variable, "dat")
419 expect_identical(input$skim_type, "POSIXct")
420 expect_identical(input$n_missing, 1L)
421 expect_equal(input$complete_rate, .9, tolerance = .001)
422 expect_identical(
423 input$POSIXct.min,
424 as.POSIXct("2011-07-01 00:00:00", tz = "UTC")
425 )
426 expect_identical(
427 input$POSIXct.max,
428 as.POSIXct("2011-07-01 00:00:09", tz = "UTC")
429 )
430 expect_identical(
431 input$POSIXct.median,
432 as.POSIXct("2011-07-01 00:00:05", tz = "UTC")
433 )
434 expect_identical(input$POSIXct.n_unique, 9L)
435 })
436
437 test_that("skim returns expected response for list (not AsIs) vectors", {
438 dat <- list(
439 list("a", "b", "c"),
440 list("d", "b", "d"),
441 list("e", "f", "g"),
442 d = list("h"),
443 e = list("i", "j", "k", "l"),
444 f = NA
445 )
446 list_frame <- tibble::tibble(dat)
447 input <- skim(list_frame)
448
449 # dimensions
450 expect_n_rows(input, 1)
451 expect_n_columns(input, 7)
452
453 # classes
454 expect_s3_class(input, c("skim_df", "tbl_df", "tbl", "data.frame"))
455 expect_named(input, c(
456 "skim_type", "skim_variable", "n_missing", "complete_rate",
457 "list.n_unique", "list.min_length", "list.max_length"
458 ))
459
460 # attributes
461 attrs <- attributes(input)
462 expect_equal(attrs$data_rows, 6)
463 expect_equal(attrs$data_cols, 1)
464 expect_equal(attrs$df_name, "`list_frame`")
465 expect_equal(
466 attrs$skimmers_used,
467 list(list = c("n_unique", "min_length", "max_length"))
468 )
469
470 # values
471 expect_identical(input$skim_variable, "dat")
472 expect_identical(input$skim_type, "list")
473 expect_identical(input$n_missing, 1L)
474 expect_equal(input$complete_rate, .833, tolerance = .001)
475 expect_identical(input$list.n_unique, 5L)
476 expect_identical(input$list.min_length, 1L)
477 expect_identical(input$list.max_length, 4L)
478 })
479
480 test_that("skim returns expected response for list with all NA's", {
481 dat <- list(NA, NA, NA)
482 list_frame <- tibble::tibble(dat)
483 input <- skim(list_frame)
484
485 # dimensions
486 expect_n_rows(input, 1)
487 expect_n_columns(input, 7)
488
489 # classes
490 expect_s3_class(input, c("skim_df", "tbl_df", "tbl", "data.frame"))
491 expect_named(input, c(
492 "skim_type", "skim_variable", "n_missing", "complete_rate",
493 "list.n_unique", "list.min_length", "list.max_length"
494 ))
495
496 # attributes
497 attrs <- attributes(input)
498 expect_equal(attrs$data_rows, 3)
499 expect_equal(attrs$data_cols, 1)
500 expect_equal(attrs$df_name, "`list_frame`")
501 expect_equal(
502 attrs$skimmers_used,
503 list(list = c("n_unique", "min_length", "max_length"))
504 )
505
506 # values
507 expect_identical(input$skim_variable, "dat")
508 expect_identical(input$skim_type, "list")
509 expect_identical(input$n_missing, 3L)
510 expect_equal(input$complete_rate, 0)
511 expect_identical(input$list.n_unique, 0L)
512 expect_identical(input$list.min_length, 1L)
513 expect_identical(input$list.max_length, 1L)
514 })
515
516 test_that("skim returns expected response for asis vectors", {
517 dat <- I(list(5, 5:6, 5:10, NA))
518 asis_frame <- tibble::tibble(dat)
519 input <- skim(asis_frame)
520
521 # dimensions
522 expect_n_rows(input, 1)
523 expect_n_columns(input, 7)
524
525 # classes
526 expect_s3_class(input, c("skim_df", "tbl_df", "tbl", "data.frame"))
527 expect_named(input, c(
528 "skim_type", "skim_variable", "n_missing", "complete_rate",
529 "AsIs.n_unique", "AsIs.min_length", "AsIs.max_length"
530 ))
531
532 # attributes
533 attrs <- attributes(input)
534 expect_equal(attrs$data_rows, 4)
535 expect_equal(attrs$data_cols, 1)
536 expect_equal(attrs$df_name, "`asis_frame`")
537 expect_equal(
538 attrs$skimmers_used,
539 list(AsIs = c("n_unique", "min_length", "max_length"))
540 )
541
542 # values
543 expect_identical(input$skim_variable, "dat")
544 expect_identical(input$skim_type, "AsIs")
545 expect_identical(input$n_missing, 1L)
546 expect_equal(input$complete_rate, .75)
547 expect_identical(input$AsIs.n_unique, 3L)
548 expect_identical(input$AsIs.min_length, 1L)
549 expect_identical(input$AsIs.max_length, 6L)
550 })
551
552 test_that("skim returns expected response for difftime vectors", {
553 dat_datetime <- as.POSIXct("2011-07-01 00:00:00", tz = "UTC")
554 dat <- difftime(dat_datetime, (dat_datetime - seq(-30, 60, 10)))
555 dat[2] <- NA
556 dt <- tibble::tibble(dat)
557 input <- skim(dt)
558
559 # dimensions
560 expect_n_rows(input, 1)
561 expect_n_columns(input, 8)
562
563 # classes
564 expect_s3_class(input, c("skim_df", "tbl_df", "tbl", "data.frame"))
565 expect_named(input, c(
566 "skim_type", "skim_variable", "n_missing", "complete_rate",
567 "difftime.min", "difftime.max", "difftime.median",
568 "difftime.n_unique"
569 ))
570
571 # attributes
572 attrs <- attributes(input)
573 expect_equal(attrs$data_rows, 10)
574 expect_equal(attrs$data_cols, 1)
575 expect_equal(attrs$df_name, "`dt`")
576 expect_equal(
577 attrs$skimmers_used,
578 list(difftime = c("min", "max", "median", "n_unique"))
579 )
580
581 # values
582 expect_identical(input$skim_variable, "dat")
583 expect_identical(input$skim_type, "difftime")
584 expect_identical(input$n_missing, 1L)
585 expect_equal(input$complete_rate, .9, tolerance = .001)
586 expect_identical(input$difftime.min, as.difftime(-30, units = "secs"))
587 expect_identical(input$difftime.max, as.difftime(60, units = "secs"))
588 expect_identical(input$difftime.median, as.difftime(20, units = "secs"))
589 expect_identical(input$difftime.n_unique, 9L)
590 })
591
592 test_that("skim returns expected response for lubridate Timespan vectors", {
593 skip_if_not_installed("lubridate")
594 dt <- tibble::tibble(x = lubridate::duration(1))
595 input <- skim(dt)
596
597 # dimensions
598 expect_n_rows(input, 1)
599 expect_n_columns(input, 8)
600
601 # classes
602 expect_s3_class(input, c("skim_df", "tbl_df", "tbl", "data.frame"))
603 expect_named(input, c(
604 "skim_type", "skim_variable", "n_missing", "complete_rate",
605 "Timespan.min", "Timespan.max", "Timespan.median",
606 "Timespan.n_unique"
607 ))
608
609 # attributes
610 attrs <- attributes(input)
611 expect_equal(attrs$data_rows, 1)
612 expect_equal(attrs$data_cols, 1)
613 expect_equal(attrs$df_name, "`dt`")
614 expect_equal(
615 attrs$skimmers_used,
616 list(Timespan = c("min", "max", "median", "n_unique"))
617 )
618
619 # values
620 expect_identical(input$skim_variable, "x")
621 expect_identical(input$skim_type, "Timespan")
622 expect_identical(input$n_missing, 0L)
623 expect_equal(input$complete_rate, 1, tolerance = .001)
624 expect_identical(input$Timespan.min, 1)
625 expect_identical(input$Timespan.max, 1)
626 expect_identical(input$Timespan.median, lubridate::duration(1))
627 expect_identical(input$Timespan.n_unique, 1L)
628 })
629
630 test_that("skim handles objects containing haven_labelled vectors: double", {
631 skip_if_not_installed("haven")
632 dt <- tibble::tibble(x = haven::labelled(c(1, 2, 3), c(A = 1, B = 2, C = 3)))
633 input <- skimr::skim(dt)
634
635 # dimensions
636 expect_n_rows(input, 1)
637 expect_n_columns(input, 12)
638
639 # classes
640 expect_s3_class(input, c("skim_df", "tbl_df", "tbl", "data.frame"))
641 expect_named(input, c(
642 "skim_type", "skim_variable", "n_missing", "complete_rate",
643 "numeric.mean", "numeric.sd", "numeric.p0", "numeric.p25",
644 "numeric.p50", "numeric.p75", "numeric.p100", "numeric.hist"
645 ))
646
647 # attributes
648 attrs <- attributes(input)
649 expect_equal(attrs$data_rows, 3)
650 expect_equal(attrs$data_cols, 1)
651 expect_equal(attrs$df_name, "`dt`")
652 expect_equal(
653 attrs$skimmers_used,
654 list(
655 numeric = c("mean", "sd", "p0", "p25", "p50", "p75", "p100", "hist")
656 )
657 )
658
659 # values
660 expect_identical(input$skim_type, "numeric")
661 expect_identical(input$skim_variable, "x")
662 expect_identical(input$n_missing, 0L)
663 expect_equal(input$complete_rate, 1)
664 expect_equal(input$numeric.mean, 2, tolerance = 0.1)
665 expect_equal(input$numeric.sd, 1, tolerance = 0.1)
666 expect_equal(input$numeric.p0, 1, tolerance = 0.1)
667 expect_equal(input$numeric.p25, 1.5, tolerance = 0.1)
668 expect_equal(input$numeric.p50, 2, tolerance = 0.1)
669 expect_equal(input$numeric.p75, 2.5, tolerance = 0.1)
670 expect_equal(input$numeric.p100, 3, tolerance = 0.1)
671 })
672
673 test_that("skim handles objects containing haven_labelled vectors: character", {
674 skip_if_not_installed("haven")
675 dt <- tibble::tibble(x = haven::labelled(
676 c("a", "b", "c"),
677 c(A = "a", B = "b", C = "c")
678 ))
679 input <- skimr::skim(dt)
680
681 # dimensions
682 expect_n_rows(input, 1)
683 expect_n_columns(input, 9)
684
685 # classes
686 expect_s3_class(input, c("skim_df", "tbl_df", "tbl", "data.frame"))
687 expect_named(input, c(
688 "skim_type", "skim_variable", "n_missing", "complete_rate",
689 "character.min", "character.max", "character.empty",
690 "character.n_unique", "character.whitespace"
691 ))
692
693 # attributes
694 attrs <- attributes(input)
695 expect_equal(attrs$data_rows, 3)
696 expect_equal(attrs$data_cols, 1)
697 expect_equal(attrs$df_name, "`dt`")
698 expect_equal(
699 attrs$skimmers_used,
700 list(character = c("min", "max", "empty", "n_unique", "whitespace"))
701 )
702
703 # values
704 expect_identical(input$skim_variable, "x")
705 expect_identical(input$skim_type, "character")
706 expect_identical(input$n_missing, 0L)
707 expect_equal(input$complete_rate, 1., tolerance = .001)
708 expect_identical(input$character.min, 1L)
709 expect_identical(input$character.max, 1L)
710 expect_identical(input$character.empty, 0L)
711 expect_identical(input$character.n_unique, 3L)
712 expect_identical(input$character.whitespace, 0L)
713 })
714
715 test_that("skim handles objects with multiple classes", {
716 dat <- seq(as.Date("2011-07-01"), by = 1, length.out = 10)
717 dat[2] <- NA
718 class(dat) <- c("strange_type", "Date")
719 x <- tibble::tibble(dat)
720 input <- skim(x)
721
722 # dimensions
723 expect_n_rows(input, 1)
724 expect_n_columns(input, 8)
725
726 # classes
727 expect_s3_class(input, c("skim_df", "tbl_df", "tbl", "data.frame"))
728 expect_named(input, c(
729 "skim_type", "skim_variable", "n_missing", "complete_rate",
730 "Date.min", "Date.max", "Date.median", "Date.n_unique"
731 ))
732
733 # values
734 expect_identical(input$skim_variable, "dat")
735 expect_identical(input$skim_type, "Date")
736 })
737
738 test_that("skim treats unknown classes as character", {
739 weight <- chickwts$weight
740 x <- tibble::tibble(weight)
741 class(x$weight) <- "strange"
742 expect_warning(input <- skim(x))
743
744 # classes
745 expect_s3_class(input, c("skim_df", "tbl_df", "tbl", "data.frame"))
746 expect_named(input, c(
747 "skim_type", "skim_variable", "n_missing", "complete_rate",
748 "character.min", "character.max", "character.empty",
749 "character.n_unique", "character.whitespace"
750 ))
751
752 # attributes
753 attrs <- attributes(input)
754 expect_equal(attrs$data_rows, 71)
755 expect_equal(attrs$data_cols, 1)
756 expect_equal(attrs$df_name, "`x`")
757 expect_equal(
758 attrs$skimmers_used,
759 list(character = c("min", "max", "empty", "n_unique", "whitespace"))
760 )
761
762 # values
763 expect_identical(input$skim_variable, "weight")
764 expect_identical(input$skim_type, "character")
765 })
766
767 test_that("skim handles objects with two unknown classes", {
768 feed <- chickwts$feed
769 x <- tibble::tibble(feed)
770 class(x$feed) <- c("strange", "stranger")
771 expect_warning(input <- skim(x))
772
773 # classes
774 expect_s3_class(input, c("skim_df", "tbl_df", "tbl", "data.frame"))
775 expect_named(input, c(
776 "skim_type", "skim_variable", "n_missing", "complete_rate",
777 "character.min", "character.max", "character.empty",
778 "character.n_unique", "character.whitespace"
779 ))
780
781 # attributes
782 attrs <- attributes(input)
783 expect_equal(attrs$data_rows, 71)
784 expect_equal(attrs$data_cols, 1)
785 expect_equal(attrs$df_name, "`x`")
786 expect_equal(
787 attrs$skimmers_used,
788 list(character = c("min", "max", "empty", "n_unique", "whitespace"))
789 )
790
791 # values
792 expect_identical(input$skim_variable, "feed")
793 expect_identical(input$skim_type, "character")
794 })
795
796 test_that("Skimming a complete data frame works as expected", {
797 input <- skim(chickwts)
798
799 # dimensions
800 expect_n_rows(input, 2)
801 expect_n_columns(input, 15)
802
803 # classes
804 expect_s3_class(input, c("skim_df", "tbl_df", "tbl", "data.frame"))
805 expect_named(input, c(
806 "skim_type", "skim_variable", "n_missing", "complete_rate",
807 "factor.ordered", "factor.n_unique", "factor.top_counts",
808 "numeric.mean", "numeric.sd", "numeric.p0", "numeric.p25", "numeric.p50",
809 "numeric.p75", "numeric.p100", "numeric.hist"
810 ))
811
812 # attributes
813 attrs <- attributes(input)
814 expect_equal(attrs$data_rows, 71)
815 expect_equal(attrs$data_cols, 2)
816 expect_equal(attrs$df_name, "`chickwts`")
817 expect_equal(attrs$base_skimmers, c("n_missing", "complete_rate"))
818 expect_equal(
819 attrs$skimmers_used,
820 list(
821 numeric = c("mean", "sd", "p0", "p25", "p50", "p75", "p100", "hist"),
822 factor = c("ordered", "n_unique", "top_counts")
823 )
824 )
825 })
826
827 test_that("successfully skim mixed data types with common skimmers", {
828 df <- data.frame(
829 Date = seq(as.Date("2011-07-01"), by = 1, length.out = 10),
830 POSIXct = as.POSIXct("2011-07-01 00:00:00", tz = "UTC")
831 )
832 input <- skim(df)
833 expect_n_rows(input, 2)
834 expect_n_columns(input, 12)
835
836 expect_s3_class(input, c("skim_df", "tbl_df", "tbl", "data.frame"))
837 expect_named(
838 input,
839 c(
840 "skim_type", "skim_variable", "n_missing", "complete_rate",
841 "Date.min", "Date.max", "Date.median", "Date.n_unique",
842 "POSIXct.min", "POSIXct.max", "POSIXct.median", "POSIXct.n_unique"
843 )
844 )
845 })
846
847 test_that("skim handles a matrix as input", {
848 expect_error(skim(volcano), NA)
849 })
850
851 test_that("skim handles a vector as input", {
852 expect_error(skim(iris$Sepal.Length), NA)
853 })
854
855 test_that("You can use tidyselect negation", {
856 input <- skim(chickwts, -weight)
857
858 # dimensions
859 expect_n_rows(input, 1)
860 expect_n_columns(input, 7)
861
862 # values
863 expect_identical(unname(input$skim_variable), "feed")
864 expect_identical(input$skim_type, "factor")
865 })
866
867 test_that("Tidyselect helpers work as expected", {
868 input <- skim(iris, starts_with("Sepal"))
869
870 expect_n_rows(input, 2)
871 expect_n_columns(input, 12)
872 expect_identical(
873 unname(input$skim_variable),
874 c("Sepal.Length", "Sepal.Width")
875 )
876 })
877
878 test_that("Tidyselect predicates work as expected", {
879 input <- skim(iris, where(is.numeric))
880
881 expect_n_rows(input, 4)
882 expect_n_columns(input, 12)
883 expect_identical(
884 unname(input$skim_variable),
885 c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")
886 )
887 })
888
889 test_that("Skimming a grouped df with selections works as expected", {
890 grouped <- dplyr::group_by(mtcars, cyl, gear)
891 input <- skim(grouped, mpg, disp)
892
893 # dimensions
894 expect_n_rows(input, 16)
895 expect_n_columns(input, 14)
896
897 # classes
898 expect_s3_class(input, c("skim_df", "tbl_df", "tbl", "data.frame"))
899 expect_named(input, c(
900 "skim_type", "skim_variable", "cyl", "gear", "n_missing",
901 "complete_rate", "numeric.mean", "numeric.sd", "numeric.p0",
902 "numeric.p25", "numeric.p50", "numeric.p75", "numeric.p100", "numeric.hist"
903 ))
904
905 # attributes
906 attrs <- attributes(input)
907 expect_equal(attrs$data_rows, 32)
908 expect_equal(attrs$data_cols, 11)
909 expect_equal(attrs$df_name, "`grouped`")
910 expect_equal(as.character(attrs$groups), c("cyl", "gear"))
911 expect_equal(
912 attrs$skimmers_used,
913 list(numeric = c("mean", "sd", "p0", "p25", "p50", "p75", "p100", "hist"))
914 )
915 })
916
917 test_that("Skimming a grouped df works when selecting exactly one variable", {
918 grouped <- dplyr::group_by(mtcars, cyl, gear)
919 input <- skim(grouped, mpg)
920
921 # dimensions
922 expect_n_rows(input, 8)
923 expect_n_columns(input, 14)
924
925 # classes
926 expect_s3_class(input, c("skim_df", "tbl_df", "tbl", "data.frame"))
927 expect_named(input, c(
928 "skim_type", "skim_variable", "cyl", "gear", "n_missing",
929 "complete_rate", "numeric.mean", "numeric.sd", "numeric.p0",
930 "numeric.p25", "numeric.p50", "numeric.p75", "numeric.p100", "numeric.hist"
931 ))
932 expect_true(all(input$skim_variable == "mpg"))
933 })
934
935 test_that("Skimming without charts produces no histograms", {
936 input <- skim_without_charts(iris, Sepal.Width)
937 expect_null(iris$numeric.hist)
938 })
939
940 test_that("Skimming without charts produces no ts line charts", {
941 input <- skim_without_charts(freeny, y)
942 expect_null(iris$ts.line_graph)
943 })
944
945 test_that("Skimming succeeds when column names are similar", {
946 input <- data.frame(
947 x = 1:10,
948 X = 11:20
949 )
950 skimmed <- skim(input)
951 expect_s3_class(skimmed, "skim_df")
952 })
953
954
955 test_that("Skim inside a function returns the data name", {
956 nested_skim <- function(df) {
957 skim(df, .data_name = deparse(substitute(df)))
958 }
959 input <- nested_skim(iris)
960 expect_equal(attr(input, "df_name"), "iris")
961 })
962
963 test_that("The data_name parameter can change the data_name attribute", {
964 input <- skim(iris, .data_name = "Anderson")
965 expect_equal(attr(input, "df_name"), "Anderson")
966 })