Debian Codegraph @

inst/other_docs/blog_v2.md

raw symbols SCIP
1 [Theme song: *PSA* by
2 Jay-Z](https://www.youtube.com/watch?v=-LzdKH1naok)
3
4 We announced the testing version of `skimr` v2 on [June 19,
5 2018](https://github.com/ropenscilabs/skimr/issues/341). After more than
6 a year of (admittedly intermittent) work, we’re thrilled to be able to
7 say that the package is ready to go to CRAN. So, what happened over the
8 last year? And why are we so excited for V2?
9
10 ## Setting the stage
11
12 Before we can talk about the last year of `skimr` development, we need
13 to lay out the timeline that got us to this point. For those deeply
14 enmeshed in `skimr` lore, all
15 [dozens](https://imgur.com/gallery/R1fdEt3) of you, bear with.
16
17 `skimr` was originally an [rOpenSci
18 unconf17](https://ropensci.org/blog/2017/07/11/skimr/) project, a big
19 collaboration between eight different participants that resulted in a
20 conceptual outline of the package and a basic working version.
21 Participating in the unconf was a truly magical experience, with
22 everyone bringing a tremendous amount of energy and ideas to the
23 project, and implementation happening over a flurry of [“fancy git
24 commits”](https://twitter.com/AmeliaMN/status/867818976666976256).
25
26 About six months later, we released our first version on CRAN. The time
27 between these two milestones was mostly spent on fleshing out all of the
28 different ideas that were generated during the unconf (like handling
29 grouped data frames) and fixing all the bugs we discovered along the
30 way.
31
32 Getting the package on CRAN opened the gates for bug reports and feature
33 requests on [GitHub](https://github.com/ropenscilabs/skimr/issues).
34 About the same time we pushed our first version to CRAN, Elin got
35 `skimr`’s rOpenSci’s package [peer
36 review](https://github.com/ropenscilabs/software-review/issues/175)
37 started (thank you Jennifer and Jim!), opening another incredibly useful
38 channel for collecting feedback on the package. All of these new ideas
39 and suggestions gave us the opportunity to really push `skimr` to the
40 next level, but doing that would require rethinking the package, from
41 the ground up.
42
43 A month after finishing the peer review (and six months after the
44 process began), we announced v2. Over the first phase of `skimr`’s life,
45 we accumulated 700 commits, two release, 400 GitHub stars, 95 percent
46 code coverage and a lifetime’s worth of [unicode rendering
47 bugs](https://github.com/ropenscilabs/skimr#support-for-spark-histograms)!
48
49 Just kidding! We love our little histograms, even when they don’t love
50 us back! For those of you that might have never seen `skimr`, using the
51 package typically boils down to a single function call:
52
53 library(skimr)
54 library(dplyr)
55 options(width = 90)
56
57 skim(iris)
58
59 ## ── Data Summary ────────────────────────
60 ## Values
61 ## Name iris
62 ## Number of rows 150
63 ## Number of columns 5
64 ## _______________________
65 ## Column type frequency:
66 ## factor 1
67 ## numeric 4
68 ## ________________________
69 ## Group variables None
70 ##
71 ## ── Variable type: factor ─────────────────────────────────────────────────────────────────
72 ## skim_variable n_missing complete_rate ordered n_unique top_counts
73 ## 1 Species 0 1 FALSE 3 set: 50, ver: 50, vir: 50
74 ##
75 ## ── Variable type: numeric ────────────────────────────────────────────────────────────────
76 ## skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
77 ## 1 Sepal.Length 0 1 5.84 0.828 4.3 5.1 5.8 6.4 7.9 ▆▇▇▅▂
78 ## 2 Sepal.Width 0 1 3.06 0.436 2 2.8 3 3.3 4.4 ▁▆▇▂▁
79 ## 3 Petal.Length 0 1 3.76 1.77 1 1.6 4.35 5.1 6.9 ▇▁▆▇▂
80 ## 4 Petal.Width 0 1 1.20 0.762 0.1 0.3 1.3 1.8 2.5 ▇▁▇▅▃
81
82 ## Getting it right
83
84 Under normal circumstances (i.e. not during a hackathon), most software
85 engineering projects begin with a design phase and series of
86 increasingly detailed design docs. `skimr` is only a few hundred lines
87 of code, which means “increasingly detailed design docs” translates to
88 one doc. But we did actually write it! [It’s
89 here](https://docs.google.com/document/d/18lBStDZzd1rJq08O-4Sw2qHhuHEZ79QX4sBkeyzWNFY/edit#heading=h.5x0d5h95i329).
90 And it still goes a good job of laying out some of the big ideas we were
91 interested in taking on for v2.
92
93 - Eliminating frictions that resulted from differences in the way we
94 stored data vs how it was displayed to users
95 - Getting away from using a global environment to configure `skimr`
96 - Making it easier for others to extend `skimr`
97 - Create more useful ways to use `skimr`
98
99 ## Better internal data structures
100
101 In v1, `skimr` stored all of its data in a “long format”, data frame.
102 Although hidden from the user by its print methods, this format would
103 appear any time you’d try do something with the results of a `skim()`
104 call. It looked something like this:
105
106 skim(mtcars) |> dplyr::filter(stat=="hist")
107
108 # A tibble: 11 x 6
109 variable type stat level value formatted
110 <chr> <chr> <chr> <chr> <dbl> <chr>
111 1 mpg numeric hist .all NA ▃▇▇▇▃▂▂▂
112 2 cyl numeric hist .all NA ▆▁▁▃▁▁▁▇
113 3 disp numeric hist .all NA ▇▆▁▂▅▃▁▂
114 4 hp numeric hist .all NA ▃▇▃▅▂▃▁▁
115 5 drat numeric hist .all NA ▃▇▁▅▇▂▁▁
116 6 wt numeric hist .all NA ▃▃▃▇▆▁▁▂
117 7 qsec numeric hist .all NA ▃▂▇▆▃▃▁▁
118 8 vs numeric hist .all NA ▇▁▁▁▁▁▁▆
119 9 am numeric hist .all NA ▇▁▁▁▁▁▁▆
120 10 gear numeric hist .all NA ▇▁▁▆▁▁▁▂
121 11 carb numeric hist .all NA ▆▇▂▇▁▁▁▁
122
123 Big ups to anyone who looked at the rendered output and saw that this
124 was how you actually filtered the results. Hopefully there are even
125 better applications of your near-telepathic abilities.
126
127 Now, working with `skimr` is a bit more sane.
128
129 skimmed <- iris |>
130 skim() |>
131 dplyr::filter(numeric.sd > 1)
132
133 skimmed
134
135 ## ── Data Summary ────────────────────────
136 ## Values
137 ## Name iris
138 ## Number of rows 150
139 ## Number of columns 5
140 ## _______________________
141 ## Column type frequency:
142 ## numeric 1
143 ## ________________________
144 ## Group variables None
145 ##
146 ## ── Variable type: numeric ────────────────────────────────────────────────────────────────
147 ## skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
148 ## 1 Petal.Length 0 1 3.76 1.77 1 1.6 4.35 5.1 6.9 ▇▁▆▇▂
149
150 And
151
152 dplyr::glimpse(skimmed)
153
154 ## Rows: 1
155 ## Columns: 15
156 ## $ skim_type <chr> "numeric"
157 ## $ skim_variable <chr> "Petal.Length"
158 ## $ n_missing <int> 0
159 ## $ complete_rate <dbl> 1
160 ## $ factor.ordered <lgl> NA
161 ## $ factor.n_unique <int> NA
162 ## $ factor.top_counts <chr> NA
163 ## $ numeric.mean <dbl> 3.758
164 ## $ numeric.sd <dbl> 1.765298
165 ## $ numeric.p0 <dbl> 1
166 ## $ numeric.p25 <dbl> 1.6
167 ## $ numeric.p50 <dbl> 4.35
168 ## $ numeric.p75 <dbl> 5.1
169 ## $ numeric.p100 <dbl> 6.9
170 ## $ numeric.hist <chr> "▇▁▆▇▂"
171
172 It’s still not perfect, as you need to rely on a *pseudo-namespace* to
173 refer to the column that you want. But this is unfortunately a necessary
174 trade-off. As the Rstats Bible, errr Hadley Wickham’s *Advanced R*,
175 states, all elements of [an atomic vector must have the same
176 type](https://adv-r.hadley.nz/vectors-chap.html). This normally isn’t
177 something that you have to think too much about, that is until you try
178 to combine the means of all your `Date` columns with the means of your
179 `numeric` columns and everything comes out utterly garbled. So instead
180 of that basket of laughs, we prefix columns names by their data type.
181
182 There’s a couple of other nuances here:
183
184 - The data frame `skim()` produces always starts off with some
185 metadata columns
186 - Functions that always produce the same, regardless of input type,
187 can be treated as `base_skimmers` and don’t need a namespace
188
189 ### Manipulating internal data
190
191 A better representation of internal data comes with better tools for
192 reshaping the data and getting it for other contexts. A common request
193 in v1 was tooling to handle the `skimr` subtables separately. We now do
194 this with `partition()`. It replaces the v1 function `skim_to_list()`.
195
196 partition(skimmed)
197
198 ## $numeric
199 ##
200 ## ── Variable type: numeric ────────────────────────────────────────────────────────────────
201 ## skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
202 ## 1 Petal.Length 0 1 3.76 1.77 1 1.6 4.35 5.1 6.9 ▇▁▆▇▂
203
204 You can undo a call to `partition()` with `bind()`, which joins the
205 subtables into the original `skim_df` object and properly accounts for
206 metadata. You can skip a step with the function `yank()`, which calls
207 partition and pulls out a particular subtable
208
209 yank(skimmed, "numeric")
210
211 ##
212 ## ── Variable type: numeric ────────────────────────────────────────────────────────────────
213 ## skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
214 ## 1 Petal.Length 0 1 3.76 1.77 1 1.6 4.35 5.1 6.9 ▇▁▆▇▂
215
216 Last, with support something close to the older format with the
217 `to_long()` function. This can be added for something close to backwards
218 compatibility. Being realistic on open source sustainability means that
219 we are not able to support 100% backward compatibility in v2 even with
220 new functions. Meanwhile you can keep using v1 if you are happy with it.
221 However, because `skimr`’s dependencies are under ongoing development,
222 sooner or later skimr v1 will no longer work with updates to them.
223
224 ### Working with dplyr
225
226 Using `skimr` in a `dplyr` pipeline was part of the original package
227 design, and we’ve needed to devote some extra love to making sure that
228 everything is as seamless as possible. Part of this is due to the object
229 produce by `skim()`, which we call `skim_df`. It’s a little weird in
230 that it needs both metadata and columns in the underlying data frame.
231
232 In practice, this means that you can coerce it into a different type
233 through normal `dplyr` operations. Here’s one:
234
235 select(skimmed, numeric.mean)
236
237 ## # A tibble: 1 × 1
238 ## numeric.mean
239 ## <dbl>
240 ## 1 3.76
241
242 To get around this, we’ve added some helper functions and methods. The
243 more `skimr`-like replacement for `select()` is `focus()`, which
244 preserves metadata columns.
245
246 focus(skimmed, numeric.mean)
247
248 ## ── Data Summary ────────────────────────
249 ## Values
250 ## Name iris
251 ## Number of rows 150
252 ## Number of columns 5
253 ## _______________________
254 ## Column type frequency:
255 ## numeric 1
256 ## ________________________
257 ## Group variables None
258 ##
259 ## ── Variable type: numeric ────────────────────────────────────────────────────────────────
260 ## skim_variable mean
261 ## 1 Petal.Length 3.76
262
263 ## Configuring and extending skimr
264
265 Most of `skimr`’s magic, to [steal a
266 term](https://resources.rstudio.com/rstudio-conf-2019/our-colour-of-magic-the-open-sourcery-of-fantastic-r-packages),
267 comes from the fact that you can do most everything with one function.
268 But believe it or not, there’s actually a bit more to the package.
269
270 One big one is customization. We like the `skimr` defaults, but that
271 doesn’t guarantee you will. So what if you want to do something
272 different, we have a function factory for that!
273
274 my_skim <- skim_with(numeric = sfl(iqr = IQR, p25 = NULL, p75 = NULL))
275 my_skim(faithful)
276
277 ## ── Data Summary ────────────────────────
278 ## Values
279 ## Name faithful
280 ## Number of rows 272
281 ## Number of columns 2
282 ## _______________________
283 ## Column type frequency:
284 ## numeric 2
285 ## ________________________
286 ## Group variables None
287 ##
288 ## ── Variable type: numeric ────────────────────────────────────────────────────────────────
289 ## skim_variable n_missing complete_rate mean sd p0 p50 p100 hist iqr
290 ## 1 eruptions 0 1 3.49 1.14 1.6 4 5.1 ▇▂▂▇▇ 2.29
291 ## 2 waiting 0 1 70.9 13.6 43 76 96 ▃▃▂▇▂ 24
292
293 Those of you familiar with customizing `skim()` in v1 will notice a
294 couple differences:
295
296 - we now has an object called `sfl()` for managing `skimr` function
297 lists; more below
298 - instead of setting global options, we now have a *function factory*
299
300 Yes! A function factory. `skim_with()` gives us a new function each time
301 we call it, and the returned function is configured by the arguments in
302 `skim_with()`. This works the same way as `ecdf()` in the `stats`
303 package or `colorRamp` in `grDevices`. Creating new functions has a few
304 advantages over the previous approach.
305
306 - you can export a `skim()` function in a package or create it in a
307 `.Rprofile`
308 - you avoid a bunch of potential side effects from setting options
309 with `skim_with()`
310
311 The other big change is how we now handle different data types. Although
312 many will never see it, a key piece of `skimr` customization comes from
313 the `get_skimmers()` generic. It’s used to detect different column types
314 in your data and set the appropriate summary functions for that type.
315 It’s also designed to work with `sfl()`. Here’s an example from the
316 “Supporting additional objects” vignette. Here, we’ll create some
317 skimmers for
318 [`sf`](https://cran.r-project.org/web/packages/sf/index.html) data
319 types:
320
321 get_skimmers.sfc_POINT <- function(column) {
322 sfl(
323 skim_type = "sfc_POINT",
324 n_unique = n_unique,
325 valid = ~ sum(sf::st_is_valid(.))
326 )
327 }
328
329 While it was required in `skim_with()`, users must provide a `skim_type`
330 value when creating new methods. With that, you can export this method
331 in a new package (be sure to import the generic), and the new default
332 skimmer is added when you load the package.
333
334 get_default_skimmer_names()
335
336 ...
337 $sfc_POINT
338 [1] "missing" "complete" "n" "n_unique" "valid"
339 ...
340
341 Even if you don’t go the full route of supporting a new data type,
342 creating a couple of `skimr` function lists has other benefits. For
343 example, you can add some to your `.Rprofile` as a way to quickly
344 configure `skimr` interactively.
345
346 sfc_point_sfl <- sfl(
347 n_unique = n_unique,
348 valid = ~ sum(sf::st_is_valid(.))
349 )
350
351 my_skimmer <- skim_with(sfc_POINT = sfc_point_sfl)
352
353 ## Using skimr in other contexts
354
355 In `skimr` v1, we developed some slightly hacky approaches to getting
356 nicer `skim()` output in RMarkdown docs. These have been removed in
357 favor of the
358 [actually-supported](https://github.com/yihui/knitr/issues/1493)
359 `knit_print` API. Now, calling `skim()`, within an RMarkdown doc should
360 produce something nice by default.
361
362 skim(chickwts)
363
364 <table>
365 <caption>Data summary</caption>
366 <tbody>
367 <tr>
368 <td style="text-align: left;">Name</td>
369 <td style="text-align: left;">chickwts</td>
370 </tr>
371 <tr>
372 <td style="text-align: left;">Number of rows</td>
373 <td style="text-align: left;">71</td>
374 </tr>
375 <tr>
376 <td style="text-align: left;">Number of columns</td>
377 <td style="text-align: left;">2</td>
378 </tr>
379 <tr>
380 <td style="text-align: left;">_______________________</td>
381 <td style="text-align: left;"></td>
382 </tr>
383 <tr>
384 <td style="text-align: left;">Column type frequency:</td>
385 <td style="text-align: left;"></td>
386 </tr>
387 <tr>
388 <td style="text-align: left;">factor</td>
389 <td style="text-align: left;">1</td>
390 </tr>
391 <tr>
392 <td style="text-align: left;">numeric</td>
393 <td style="text-align: left;">1</td>
394 </tr>
395 <tr>
396 <td style="text-align: left;">________________________</td>
397 <td style="text-align: left;"></td>
398 </tr>
399 <tr>
400 <td style="text-align: left;">Group variables</td>
401 <td style="text-align: left;">None</td>
402 </tr>
403 </tbody>
404 </table>
405
406 **Variable type: factor**
407
408 <table>
409 <colgroup>
410 <col style="width: 15%" />
411 <col style="width: 11%" />
412 <col style="width: 15%" />
413 <col style="width: 8%" />
414 <col style="width: 10%" />
415 <col style="width: 38%" />
416 </colgroup>
417 <thead>
418 <tr>
419 <th style="text-align: left;">skim_variable</th>
420 <th style="text-align: right;">n_missing</th>
421 <th style="text-align: right;">complete_rate</th>
422 <th style="text-align: left;">ordered</th>
423 <th style="text-align: right;">n_unique</th>
424 <th style="text-align: left;">top_counts</th>
425 </tr>
426 </thead>
427 <tbody>
428 <tr>
429 <td style="text-align: left;">feed</td>
430 <td style="text-align: right;">0</td>
431 <td style="text-align: right;">1</td>
432 <td style="text-align: left;">FALSE</td>
433 <td style="text-align: right;">6</td>
434 <td style="text-align: left;">soy: 14, cas: 12, lin: 12, sun: 12</td>
435 </tr>
436 </tbody>
437 </table>
438
439 **Variable type: numeric**
440
441 <table style="width:100%;">
442 <colgroup>
443 <col style="width: 17%" />
444 <col style="width: 12%" />
445 <col style="width: 17%" />
446 <col style="width: 8%" />
447 <col style="width: 7%" />
448 <col style="width: 4%" />
449 <col style="width: 7%" />
450 <col style="width: 4%" />
451 <col style="width: 7%" />
452 <col style="width: 6%" />
453 <col style="width: 7%" />
454 </colgroup>
455 <thead>
456 <tr>
457 <th style="text-align: left;">skim_variable</th>
458 <th style="text-align: right;">n_missing</th>
459 <th style="text-align: right;">complete_rate</th>
460 <th style="text-align: right;">mean</th>
461 <th style="text-align: right;">sd</th>
462 <th style="text-align: right;">p0</th>
463 <th style="text-align: right;">p25</th>
464 <th style="text-align: right;">p50</th>
465 <th style="text-align: right;">p75</th>
466 <th style="text-align: right;">p100</th>
467 <th style="text-align: left;">hist</th>
468 </tr>
469 </thead>
470 <tbody>
471 <tr>
472 <td style="text-align: left;">weight</td>
473 <td style="text-align: right;">0</td>
474 <td style="text-align: right;">1</td>
475 <td style="text-align: right;">261.31</td>
476 <td style="text-align: right;">78.07</td>
477 <td style="text-align: right;">108</td>
478 <td style="text-align: right;">204.5</td>
479 <td style="text-align: right;">258</td>
480 <td style="text-align: right;">323.5</td>
481 <td style="text-align: right;">423</td>
482 <td style="text-align: left;">▆▆▇▇▃</td>
483 </tr>
484 </tbody>
485 </table>
486
487 You get a nice html version of both the summary header and the `skimr`
488 subtables for each type of data.
489
490 In this context, you configure the output the same way you handle other
491 `knitr` code chunks.
492
493 This means that we’re dropping direct support for `kable.skim_df()` and
494 `pander.skim_df()`. But you can still get pretty similar results to
495 these functions by using the reshaping functions described above to get
496 subtables. You can also still use `Pander` and other nice rendering
497 packages on an ad hoc basis as you would for other data frames or
498 tibbles.
499
500 We also have a similarly-nice rendered output in
501 [Jupyter](https://github.com/ropenscilabs/skimr/blob/8c2263c4fd4796af0e5e8f32aafc4980bd58d43a/inst/other_docs/skimr_in_jupyter.ipynb)
502 and RMarkdown notebooks. In the latter, the summary is separated from
503 the rest of the output when working interactively. We like it that way,
504 but we’d be happy to hear what the rest of you think!
505
506 ## Wait, that took over a year?
507
508 Well, we think that’s a lot! But to be fair, it wasn’t exactly simple to
509 keep up with `skimr`. Real talk, open source development takes up a lot
510 of time, and the `skimr` developers have additional important
511 priorities. Michael’s family added a new baby, and despite swearing up
512 and down otherwise, he got absolutely nothing not-baby-related done
513 during his paternity leave (take note new dads!). Elin ended up taking a
514 much bigger role on at Lehman, really limiting time for any other work.
515
516 Even so, these are just the highlights in the normal ebb and flow of
517 this sort of work. Since it’s no one’s real job, it might not always be
518 the first focus. And that’s OK! We’ve been really lucky to have a group
519 of new users that have been very patient with this slow development
520 cycle while still providing really good feedback throughout. Thank you
521 all!
522
523 We’re really excited about this next step in the `skimr` journey. We’ve
524 put a huge amount of work into this new version. Hopefully it shows. And
525 hopefully it inspires some of you to send more feedback and help us find
526 even more ways to improve!
527
528 Updated to use base pipe and update URLS.