Debian Codegraph @

tests/testthat/test-skim_with.R

raw
1 test_that("Skimmer list is updated correctly when changing functions", {
2 funs <- sfl(median = median, mad = mad)
3 new_skim <- skim_with(numeric = funs, append = FALSE)
4 x <- tibble::tibble(rnorm(10))
5 input <- new_skim(x)
6 used <- attr(input, "skimmers_used")
7 expect_identical(used, list(numeric = c("median", "mad")))
8 })
9
10 test_that("Skimming functions can be changed for multiple types", {
11 newfuns1 <- sfl(iqr = IQR, q99 = ~ quantile(., probs = .99))
12 newfuns2 <- sfl(n2 = length)
13 new_skim <- skim_with(numeric = newfuns1, factor = newfuns2, append = FALSE)
14 input <- new_skim(iris)
15 used <- attr(input, "skimmers_used")
16 expect_identical(used, list(numeric = c("iqr", "q99"), factor = "n2"))
17 })
18
19 test_that("Skimming functions can be changed with a list", {
20 newfuns1 <- sfl(iqr = IQR, q99 = ~ quantile(., probs = .99))
21 new_skim <- skim_with(list(numeric = newfuns1), append = FALSE)
22 input <- new_skim(iris)
23 used <- attr(input, "skimmers_used")
24 expect_identical(used$numeric, c("iqr", "q99"))
25 })
26
27 test_that("Skimming functions can be changed with a list", {
28 newfuns1 <- sfl(iqr = IQR, q99 = ~ quantile(., probs = .99))
29 newfuns2 <- sfl(n2 = length)
30 new_skim <- skim_with(
31 list(numeric = newfuns1, factor = newfuns2),
32 append = FALSE
33 )
34 input <- new_skim(iris)
35 used <- attr(input, "skimmers_used")
36 expect_identical(
37 used, list(numeric = c("iqr", "q99"), factor = c("n2"))
38 )
39 })
40
41 test_that("Skimming functions can be appended.", {
42 funs <- sfl(iqr = IQR)
43 new_skim <- skim_with(numeric = funs)
44 x <- tibble::tibble(rnorm(10))
45 input <- new_skim(x)
46 used <- attr(input, "skimmers_used")
47 expect_identical(
48 used,
49 list(
50 numeric = c(
51 "mean", "sd", "p0", "p25", "p50", "p75", "p100", "hist", "iqr"
52 )
53 )
54 )
55 })
56
57 test_that("Setting a statistic to null removes it from skimmers", {
58 new_skim <- skim_with(numeric = sfl(hist = NULL))
59 x <- tibble::tibble(rnorm(10))
60 input <- new_skim(x)
61 used <- attr(input, "skimmers_used")
62 expect_identical(
63 used, list(numeric = c("mean", "sd", "p0", "p25", "p50", "p75", "p100"))
64 )
65 })
66
67 test_that("Skimmers can be removed and added at the same time", {
68 new_skim <- skim_with(numeric = sfl(hist = NULL, iqr = IQR))
69 x <- tibble::tibble(rnorm(10))
70 input <- new_skim(x)
71 used <- attr(input, "skimmers_used")
72 expect_identical(
73 used,
74 list(numeric = c("mean", "sd", "p0", "p25", "p50", "p75", "p100", "iqr"))
75 )
76 })
77
78 test_that("Skimming functions for new types can be added", {
79 funs <- sfl(iqr = IQR, quantile = ~ quantile(., probs = .99))
80 expect_message(new_skim <- skim_with(new_type = funs), "new_type")
81 x <- tibble::tibble(x = rnorm(10))
82 class(x$x) <- "new_type"
83 input <- new_skim(x)
84 used <- attr(input, "skimmers_used")
85 expect_identical(used, list(new_type = c("iqr", "quantile")))
86 })
87
88 test_that("Set multiple sets of skimming functions", {
89 funs <- sfl(iqr = IQR, quantile = ~ quantile(., probs = .99))
90 expect_message(
91 new_skim <- skim_with(numeric = funs, new_type = funs),
92 "new_type"
93 )
94 x <- tibble::tibble(x = rnorm(10), y = rnorm(10))
95 class(x$x) <- "new_type"
96 input <- new_skim(x)
97 used <- attr(input, "skimmers_used")
98 expect_named(used, c("new_type", "numeric"))
99 expect_identical(used$new_type, c("iqr", "quantile"))
100 expect_identical(
101 used$numeric,
102 c(
103 "mean", "sd", "p0", "p25", "p50", "p75", "p100", "hist", "iqr", "quantile"
104 )
105 )
106 })
107
108
109 test_that("Set multiple sets of skimming functions, rlang", {
110 funs <- sfl(iqr = IQR, quantile = ~ quantile(., probs = .99))
111 expect_message(new_skim <- skim_with(!!!list(numeric = funs, new_type = funs),
112 append = FALSE
113 ))
114 x <- tibble::tibble(x = rnorm(10), y = rnorm(10))
115 class(x$x) <- "new_type"
116 input <- new_skim(x)
117 used <- attr(input, "skimmers_used")
118 expect_named(used, c("new_type", "numeric"))
119 expect_identical(used$new_type, c("iqr", "quantile"))
120 expect_identical(used$numeric, c("iqr", "quantile"))
121 })
122
123 test_that("Skimming functions without a class return a message.", {
124 funs_no_class <- sfl(IQR)
125 expect_error(skim_with(funs_no_class), "arguments to be named.")
126 expect_error(
127 skim_with(funs_no_class, numeric = funs_no_class),
128 "arguments to be named."
129 )
130 })
131
132 test_that("An empty call to skim_with() returns the default skim()", {
133 input <- skim_with()
134 expect_identical(input(iris), skim(iris))
135 })
136
137 test_that("User-defined defaults require sfl's with class names", {
138 local_mocked_bindings(get_skimmers = function(column) sfl(length))
139 expect_error(skim(data.frame(1)), "Default skimming functions")
140 })
141
142 test_that("Sfl's can be passed as an unquoted list", {
143 my_skimmers <- list(numeric = sfl(mean), factor = sfl(length))
144 my_skim <- skim_with(!!!my_skimmers, append = FALSE)
145 input <- my_skim(iris)
146 expect_named(
147 input,
148 c(
149 "skim_type", "skim_variable", "n_missing", "complete_rate",
150 "factor.length", "numeric.mean"
151 )
152 )
153 })
154
155 test_that("Doubles and integers are both 'numeric'", {
156 df <- data.frame(int = 1:3, dbl = 1:3 + 0.5)
157 my_skim <- skim_with(numeric = sfl(hist = NULL))
158 input <- my_skim(df)
159
160 expect_false("numeric.hist" %in% names(input))
161 expect_equal(
162 attr(input, "skimmers_used")$numeric,
163 c("mean", "sd", "p0", "p25", "p50", "p75", "p100")
164 )
165 })
166
167 test_that("Defining an integer sfl changes behavior", {
168 df <- data.frame(int = 1:3, dbl = 1:3 + 0.5)
169 expect_message(
170 my_skim <- skim_with(
171 numeric = sfl(hist = NULL), integer = sfl(int_mean = mean)
172 )
173 )
174 input <- my_skim(df)
175
176 expect_false("numeric.hist" %in% names(input))
177 expect_true("integer.int_mean" %in% names(input))
178 expect_identical(
179 attr(input, "skimmers_used"),
180 list(
181 integer = c("int_mean"),
182 numeric = c("mean", "sd", "p0", "p25", "p50", "p75", "p100")
183 )
184 )
185 })
186
187 test_that("Base skimmers can be changed", {
188 my_skim <- skim_with(base = sfl(length = length))
189 skimmed <- my_skim(iris)
190 expect_true("length" %in% names(skimmed))
191 expect_equal(attr(skimmed, "base_skimmers"), "length")
192 })
193
194 test_that("Base skimmers require an sfl", {
195 expect_error(skim_with(base = list(length = length)))
196 })
197
198 test_that("Base skimmers can be removed", {
199 my_skim <- skim_with(base = NULL)
200 skimmed <- my_skim(iris)
201 used <- attr(skimmed, "skimmers_used")
202 expect_false("base" %in% names(used))
203 })