Debian Codegraph @

inst/other_docs/blog_v2.Rmd

raw
1 ---
2 title: "(Re)introducing `skimr` v2"
3 slug: skimrv2
4 date: "10/09/2019"
5 package_version: 2
6 authors:
7 - name: Michael Quinn
8 url: https://github.com/michaelquinn32
9 - name: Elin Waring
10 url: https://github.com/elinw
11 subtitle: A year in the life of an open source R project
12 categories: blog
13 tags:
14 - software peer review
15 - R
16 - packages
17 - community
18 - skimr
19 ---
20
21 [Theme song: *PSA* by Jay-Z](https://www.youtube.com/watch?v=-LzdKH1naok)
22
23 We announced the testing version of `skimr` v2 on
24 [June 19, 2018](https://github.com/ropenscilabs/skimr/issues/341). After more than
25 a year of (admittedly intermittent) work, we're thrilled to be able to say that
26 the package is ready to go to CRAN. So, what happened over the last year? And
27 why are we so excited for V2?
28
29 ## Setting the stage
30
31 Before we can talk about the last year of `skimr` development, we need to lay
32 out the timeline that got us to this point. For those deeply enmeshed in `skimr`
33 lore, all [dozens](https://imgur.com/gallery/R1fdEt3) of you, bear with.
34
35 `skimr` was originally an [rOpenSci
36 unconf17](https://ropensci.org/blog/2017/07/11/skimr/) project, a big
37 collaboration between eight different participants that resulted in a conceptual
38 outline of the package and a basic working version. Participating in the unconf
39 was a truly magical experience, with everyone bringing a tremendous amount of
40 energy and ideas to the project, and implementation happening over a flurry of
41 ["fancy git commits"](https://twitter.com/AmeliaMN/status/867818976666976256).
42
43 About six months later, we released our first version on CRAN. The time between
44 these two milestones was mostly spent on fleshing out all of the different ideas
45 that were generated during the unconf (like handling grouped data frames) and
46 fixing all the bugs we discovered along the way.
47
48 Getting the package on CRAN opened the gates for bug reports and feature
49 requests on [GitHub](https://github.com/ropenscilabs/skimr/issues). About the same
50 time we pushed our first version to CRAN, Elin got `skimr`'s rOpenSci's package
51 [peer review](https://github.com/ropenscilabs/software-review/issues/175) started
52 (thank you Jennifer and Jim!), opening another incredibly useful channel for
53 collecting feedback on the package. All of these new ideas and suggestions gave
54 us the opportunity to really push `skimr` to the next level, but doing that
55 would require rethinking the package, from the ground up.
56
57 A month after finishing the peer review (and six months after the process
58 began), we announced v2. Over the first phase of `skimr`'s life, we accumulated
59 700 commits, two release, 400 GitHub stars, 95 percent code coverage and a
60 lifetime's worth of [unicode rendering
61 bugs](https://github.com/ropenscilabs/skimr#support-for-spark-histograms)!
62
63 Just kidding! We love our little histograms, even when they don't love us back!
64 For those of you that might have never seen `skimr`, using the package typically
65 boils down to a single function call:
66
67 ```{r render = knitr::normal_print, message=FALSE}
68 library(skimr)
69 library(dplyr)
70 options(width = 90)
71
72 skim(iris)
73 ```
74
75 ## Getting it right
76
77 Under normal circumstances (i.e. not during a hackathon), most software
78 engineering projects begin with a design phase and series of increasingly
79 detailed design docs. `skimr` is only a few hundred lines of code, which means
80 "increasingly detailed design docs" translates to one doc. But we did actually
81 write it! [It's
82 here](https://docs.google.com/document/d/18lBStDZzd1rJq08O-4Sw2qHhuHEZ79QX4sBkeyzWNFY/edit#heading=h.5x0d5h95i329).
83 And it still goes a good job of laying out some of the big ideas we were
84 interested in taking on for v2.
85
86 * Eliminating frictions that resulted from differences in the way we stored data
87 vs how it was displayed to users
88 * Getting away from using a global environment to configure `skimr`
89 * Making it easier for others to extend `skimr`
90 * Create more useful ways to use `skimr`
91
92 ## Better internal data structures
93
94 In v1, `skimr` stored all of its data in a "long format", data frame. Although
95 hidden from the user by its print methods, this format would appear any time
96 you'd try do something with the results of a `skim()` call. It looked something
97 like this:
98
99 ```{r eval = FALSE}
100 skim(mtcars) |> dplyr::filter(stat=="hist")
101 ```
102 ```
103 # A tibble: 11 x 6
104 variable type stat level value formatted
105 <chr> <chr> <chr> <chr> <dbl> <chr>
106 1 mpg numeric hist .all NA ▃▇▇▇▃▂▂▂
107 2 cyl numeric hist .all NA ▆▁▁▃▁▁▁▇
108 3 disp numeric hist .all NA ▇▆▁▂▅▃▁▂
109 4 hp numeric hist .all NA ▃▇▃▅▂▃▁▁
110 5 drat numeric hist .all NA ▃▇▁▅▇▂▁▁
111 6 wt numeric hist .all NA ▃▃▃▇▆▁▁▂
112 7 qsec numeric hist .all NA ▃▂▇▆▃▃▁▁
113 8 vs numeric hist .all NA ▇▁▁▁▁▁▁▆
114 9 am numeric hist .all NA ▇▁▁▁▁▁▁▆
115 10 gear numeric hist .all NA ▇▁▁▆▁▁▁▂
116 11 carb numeric hist .all NA ▆▇▂▇▁▁▁▁
117 ```
118
119 Big ups to anyone who looked at the rendered output and saw that this was how
120 you actually filtered the results. Hopefully there are even better applications
121 of your near-telepathic abilities.
122
123 Now, working with `skimr` is a bit more sane.
124
125 ```{r render = knitr::normal_print}
126 skimmed <- iris |>
127 skim() |>
128 dplyr::filter(numeric.sd > 1)
129
130 skimmed
131 ```
132
133 And
134
135 ```{r render = knitr::normal_print}
136 dplyr::glimpse(skimmed)
137 ```
138
139 It's still not perfect, as you need to rely on a *pseudo-namespace* to refer to
140 the column that you want. But this is unfortunately a necessary trade-off. As
141 the Rstats Bible, errr Hadley Wickham's *Advanced R*, states, all elements of
142 [an atomic vector must have the same
143 type](https://adv-r.hadley.nz/vectors-chap.html). This normally isn't something
144 that you have to think too much about, that is until you try to combine the
145 means of all your `Date` columns with the means of your `numeric` columns and
146 everything comes out utterly garbled. So instead of that basket of laughs, we
147 prefix columns names by their data type.
148
149 There's a couple of other nuances here:
150
151 * The data frame `skim()` produces always starts off with some metadata columns
152 * Functions that always produce the same, regardless of input type, can
153 be treated as `base_skimmers` and don't need a namespace
154
155 ### Manipulating internal data
156
157 A better representation of internal data comes with better tools for reshaping
158 the data and getting it for other contexts. A common request in v1 was tooling
159 to handle the `skimr` subtables separately. We now do this with `partition()`.
160 It replaces the v1 function `skim_to_list()`.
161
162 ```{r render = knitr::normal_print}
163 partition(skimmed)
164 ```
165
166 You can undo a call to `partition()` with `bind()`, which joins the subtables
167 into the original `skim_df` object and properly accounts for metadata. You
168 can skip a step with the function `yank()`, which calls partition and pulls
169 out a particular subtable
170
171
172 ```{r render = knitr::normal_print}
173 yank(skimmed, "numeric")
174 ```
175
176 Last, with support something close to the older format with the `to_long()`
177 function. This can be added for something close to backwards compatibility.
178 Being realistic on open source sustainability means that we are not able to
179 support 100% backward compatibility in v2 even with new functions. Meanwhile
180 you can keep using v1 if you are happy with it. However,
181 because `skimr`'s dependencies are under ongoing development, sooner or later
182 skimr v1 will no longer work with updates to them.
183
184 ### Working with dplyr
185
186 Using `skimr` in a `dplyr` pipeline was part of the original package design, and
187 we've needed to devote some extra love to making sure that everything is as
188 seamless as possible. Part of this is due to the object produce by `skim()`,
189 which we call `skim_df`. It's a little weird in that it needs both metadata and
190 columns in the underlying data frame.
191
192 In practice, this means that you can coerce it into a different type through
193 normal `dplyr` operations. Here's one:
194
195 ```{r render = knitr::normal_print}
196 select(skimmed, numeric.mean)
197 ```
198
199 To get around this, we've added some helper functions and methods. The more
200 `skimr`-like replacement for `select()` is `focus()`, which preserves
201 metadata columns.
202
203 ```{r render = knitr::normal_print}
204 focus(skimmed, numeric.mean)
205 ```
206
207 ## Configuring and extending skimr
208
209 Most of `skimr`'s magic, to [steal a
210 term](https://resources.rstudio.com/rstudio-conf-2019/our-colour-of-magic-the-open-sourcery-of-fantastic-r-packages),
211 comes from the fact that you can do
212 most everything with one function. But believe it or not, there's actually a bit
213 more to the package.
214
215 One big one is customization. We like the `skimr` defaults, but that doesn't
216 guarantee you will. So what if you want to do something different, we have
217 a function factory for that!
218
219 ```{r render = knitr::normal_print}
220 my_skim <- skim_with(numeric = sfl(iqr = IQR, p25 = NULL, p75 = NULL))
221 my_skim(faithful)
222 ```
223
224 Those of you familiar with customizing `skim()` in v1 will notice a couple
225 differences:
226
227 * we now has an object called `sfl()` for managing `skimr` function lists; more
228 below
229 * instead of setting global options, we now have a *function factory*
230
231 Yes! A function factory. `skim_with()` gives us a new function each time
232 we call it, and the returned function is configured by the arguments in
233 `skim_with()`. This works the same way as `ecdf()` in the `stats` package or
234 `colorRamp` in `grDevices`. Creating new functions has a few advantages over
235 the previous approach.
236
237 * you can export a `skim()` function in a package or create it in a `.Rprofile`
238 * you avoid a bunch of potential side effects from setting options with
239 `skim_with()`
240
241 The other big change is how we now handle different data types. Although many
242 will never see it, a key piece of `skimr` customization comes from the
243 `get_skimmers()` generic. It's used to detect different column types in your
244 data and set the appropriate summary functions for that type. It's also
245 designed to work with `sfl()`. Here's an example from the "Supporting additional
246 objects" vignette. Here, we'll create some skimmers for
247 [`sf`](https://cran.r-project.org/web/packages/sf/index.html) data types:
248
249 ```{r eval = FALSE}
250 get_skimmers.sfc_POINT <- function(column) {
251 sfl(
252 skim_type = "sfc_POINT",
253 n_unique = n_unique,
254 valid = ~ sum(sf::st_is_valid(.))
255 )
256 }
257 ```
258
259 While it was required in `skim_with()`, users must provide a `skim_type` value
260 when creating new methods. With that, you can export this method in a new
261 package (be sure to import the generic), and the new default skimmer is added
262 when you load the package.
263
264 ```{r, eval = FALSE}
265 get_default_skimmer_names()
266 ```
267 ```
268 ...
269 $sfc_POINT
270 [1] "missing" "complete" "n" "n_unique" "valid"
271 ...
272 ```
273
274 Even if you don't go the full route of supporting a new data type, creating a
275 couple of `skimr` function lists has other benefits. For example, you can add
276 some to your `.Rprofile` as a way to quickly configure `skimr` interactively.
277
278 ```{r eval = FALSE}
279 sfc_point_sfl <- sfl(
280 n_unique = n_unique,
281 valid = ~ sum(sf::st_is_valid(.))
282 )
283
284 my_skimmer <- skim_with(sfc_POINT = sfc_point_sfl)
285 ```
286
287 ## Using skimr in other contexts
288
289 In `skimr` v1, we developed some slightly hacky approaches to getting nicer
290 `skim()` output in RMarkdown docs. These have been removed in favor of the
291 [actually-supported](https://github.com/yihui/knitr/issues/1493) `knit_print`
292 API. Now, calling `skim()`, within an RMarkdown doc should produce something
293 nice by default.
294
295 ```{r}
296 skim(chickwts)
297 ```
298
299 You get a nice html version of both the summary header and the `skimr` subtables
300 for each type of data.
301
302 In this context, you configure the output the same way you handle other `knitr`
303 code chunks.
304
305 ~~~
306 ```{r skimr_digits = 4, skimr_summary = TRUE}
307 ```
308 ~~~
309
310 This means that we're dropping direct support for `kable.skim_df()` and
311 `pander.skim_df()`. But you can still get pretty similar results to these
312 functions by using the reshaping functions described above to get subtables. You
313 can also still use `Pander` and other nice rendering packages on an ad hoc basis
314 as you would for other data frames or tibbles.
315
316 We also have a similarly-nice rendered output in
317 [Jupyter](https://github.com/ropenscilabs/skimr/blob/8c2263c4fd4796af0e5e8f32aafc4980bd58d43a/inst/other_docs/skimr_in_jupyter.ipynb)
318 and RMarkdown notebooks. In the latter, the summary is separated from the rest
319 of the output when working interactively. We like it that way, but we'd be happy
320 to hear what the rest of you think!
321
322 ## Wait, that took over a year?
323
324 Well, we think that's a lot! But to be fair, it wasn't exactly simple to keep up
325 with `skimr`. Real talk, open source development takes up a lot of time, and
326 the `skimr` developers have additional important priorities. Michael's family
327 added a new baby, and despite swearing up and down otherwise,
328 he got absolutely nothing not-baby-related done during his paternity leave (take
329 note new dads!). Elin
330 ended up taking a much bigger role on at Lehman, really limiting time for any
331 other work.
332
333 Even so, these are just the highlights in the normal ebb and flow of this sort
334 of work. Since it's no one's real job, it might not always be the first focus.
335 And that's OK! We've been really lucky to have a group of new users that have
336 been very patient with this slow development cycle while still providing really
337 good feedback throughout. Thank you all!
338
339 We're really excited about this next step in the `skimr` journey. We've put a
340 huge amount of work into this new version. Hopefully it shows. And hopefully
341 it inspires some of you to send more feedback and help us find even more ways
342 to improve!
343
344 Updated to use base pipe and update URLS.