| 1 |
--- |
| 2 |
title: "Extending skimr" |
| 3 |
date: "`r Sys.Date()`" |
| 4 |
output: rmarkdown::html_vignette |
| 5 |
vignette: > |
| 6 |
%\VignetteIndexEntry{Extending skimr} |
| 7 |
%\VignetteEngine{knitr::rmarkdown} |
| 8 |
%\VignetteEncoding{UTF-8} |
| 9 |
--- |
| 10 |
|
| 11 |
## Introduction |
| 12 |
|
| 13 |
The `skim()` function summarizes data types contained within data frames and |
| 14 |
objects that have `as.data.frame()` methods to coerce them into data frames. It |
| 15 |
comes with a set of default summary functions for a wide variety of data types, |
| 16 |
but this is not comprehensive. |
| 17 |
|
| 18 |
Package authors (and advanced users) can add support for skimming their specific |
| 19 |
non-data-frame objects in their packages, and they can provide different |
| 20 |
defaults in their own summary functions. This will require including skimr as a |
| 21 |
dependency. |
| 22 |
|
| 23 |
# Skimming objects that are not coercible to data frames |
| 24 |
|
| 25 |
This example will illustrate this by creating support for the `lm` object |
| 26 |
produced by `lm()`. For any object this involves two required elements and one |
| 27 |
optional element. This is a simple example, but for other types of objects |
| 28 |
there may be much more complexity |
| 29 |
|
| 30 |
If you are adding skim support to a package you will also need to add `skimr` to |
| 31 |
the list of imports. |
| 32 |
|
| 33 |
```{r} |
| 34 |
library(skimr) |
| 35 |
``` |
| 36 |
|
| 37 |
The `lm()` function produces a complex object with class "lm". |
| 38 |
|
| 39 |
```{r} |
| 40 |
results <- lm(weight ~ feed, data = chickwts) |
| 41 |
class(results) |
| 42 |
attributes(results) |
| 43 |
``` |
| 44 |
|
| 45 |
There is no as.data.frame method for an `lm` object. |
| 46 |
|
| 47 |
```{r, eval = FALSE} |
| 48 |
as.data.frame(results) |
| 49 |
#> Error in as.data.frame.default(results) : |
| 50 |
#> cannot coerce class ‘"lm"’ to a data.frame |
| 51 |
``` |
| 52 |
|
| 53 |
|
| 54 |
Unlike the example of having a new type of data in a column of a simple data |
| 55 |
frame (for which we would create a `sfl`) frame in the "Using skimr" vignette, |
| 56 |
this is a different type of challenge: an object that we might wish to skim, but |
| 57 |
that cannot be directly skimmed. Therefore we need to make it into an object |
| 58 |
that is either a data frame or coercible to a data frame. |
| 59 |
|
| 60 |
In the case of the lm object, the `model` attribute is already a data frame. So |
| 61 |
a very simple way to solve the challenge is to skim `results$model` directly. |
| 62 |
|
| 63 |
```{r} |
| 64 |
skim(results$model) |
| 65 |
``` |
| 66 |
|
| 67 |
This is works, but we could go one step further and create a new function for |
| 68 |
doing this directly. |
| 69 |
|
| 70 |
```{r} |
| 71 |
skim_lm <- function(.data) { |
| 72 |
.data <- .data$model |
| 73 |
skimr::skim(.data) |
| 74 |
} |
| 75 |
|
| 76 |
lm(weight ~ feed, data = chickwts) |> skim_lm() |
| 77 |
``` |
| 78 |
|
| 79 |
If desired, a more complex function can be created. For example, the lm object |
| 80 |
also contains fitted values and residuals. We could incorporate these in the |
| 81 |
data frame. |
| 82 |
|
| 83 |
```{r} |
| 84 |
skim_lm <- function(.data, fit = FALSE) { |
| 85 |
.data <- .data$model |
| 86 |
if (fit) { |
| 87 |
.data <- .data |> |
| 88 |
dplyr::bind_cols( |
| 89 |
fitted = data.frame(results$fitted.values), |
| 90 |
residuals = data.frame(results$residuals) |
| 91 |
) |
| 92 |
} |
| 93 |
skimr::skim(.data) |
| 94 |
} |
| 95 |
``` |
| 96 |
|
| 97 |
|
| 98 |
```{r} |
| 99 |
skim_lm(results, fit = TRUE) |
| 100 |
``` |
| 101 |
|
| 102 |
A second example of the need for a special function is with `dist` objects. The |
| 103 |
`UScitiesD` data set is an example of this. |
| 104 |
|
| 105 |
```{r} |
| 106 |
class(UScitiesD) |
| 107 |
UScitiesD |
| 108 |
``` |
| 109 |
|
| 110 |
A `dist` object is most often, as in this case, lower triange matrices of |
| 111 |
distances, which can be measured in various ways. There are many packages that |
| 112 |
produce dist objects and/or take dist objects as inputs, including those for |
| 113 |
cluster analysis and multidimensional scaling. |
| 114 |
|
| 115 |
A simple solution to this is to follow a similar design to that for `lm` |
| 116 |
objects. |
| 117 |
|
| 118 |
```{r} |
| 119 |
skim_dist <- function(.data) { |
| 120 |
.data <- data.frame(as.matrix(.data)) |
| 121 |
skimr::skim(.data) |
| 122 |
} |
| 123 |
``` |
| 124 |
|
| 125 |
However, this has the limitation of treating the dist data as though it is |
| 126 |
simple numeric data. |
| 127 |
|
| 128 |
What we might want to do, instead, is to create a new class, for example, |
| 129 |
"distance" that is specifically for distance data. This will allow it to have |
| 130 |
its own `sfl` or skimr function list. |
| 131 |
|
| 132 |
As handling gets more complex, rather than make a new function it can be more |
| 133 |
powerful to define an `as.data.frame` S3 method for dist objects, which will |
| 134 |
allow it to integrate with skimr more completely and uses to use the `skim()` |
| 135 |
function directly. In a package you will want to export this. |
| 136 |
|
| 137 |
```{r} |
| 138 |
as.data.frame.dist <- function(.data) { |
| 139 |
.data <- data.frame(as.matrix(.data)) |
| 140 |
|
| 141 |
.data[] <- lapply(.data, structure, class = "distance", nms = names(.data)) |
| 142 |
.data |
| 143 |
} |
| 144 |
|
| 145 |
as.data.frame(UScitiesD) |
| 146 |
``` |
| 147 |
|
| 148 |
However, until an `sfl` is created, `skimr` will not recognize the class and |
| 149 |
fall back to treating the data as if it were character data. |
| 150 |
|
| 151 |
```{r} |
| 152 |
skim(UScitiesD) |
| 153 |
``` |
| 154 |
|
| 155 |
The solution to this is to define an `sfl` (skimr function list) specifically |
| 156 |
for the `distance` class. |
| 157 |
|
| 158 |
## Defining sfl's for a package |
| 159 |
|
| 160 |
`skimr` has an opinionated list of functions for each class (e.g. numeric, |
| 161 |
factor) of data. The core package supports many commonly used classes, but |
| 162 |
there are many others. You can investigate these defaults by calling |
| 163 |
`get_default_skimmer_names()`. |
| 164 |
|
| 165 |
What if your data type, like `distance`, isn't covered by defaults? `skimr` |
| 166 |
usually falls back to treating the type as a character, which isn't necessarily |
| 167 |
helpful. In this case, you're best off adding your data type with `skim_with()`. |
| 168 |
|
| 169 |
Before we begin, we'll be using the following custom summary statistics |
| 170 |
throughout. These functions find the nearest and furthest other location for |
| 171 |
each location. |
| 172 |
|
| 173 |
One thing that is important to be aware of when creating statistics functions |
| 174 |
for skimr is that skimr largely uses tibbles rather than base data frames. This |
| 175 |
means that many base operations do not work as expected. |
| 176 |
|
| 177 |
```{r} |
| 178 |
get_nearest <- function(column) { |
| 179 |
closest <- which.min(column[column != 0]) |
| 180 |
cities <- attr(column, "nms")[column != 0] |
| 181 |
toString(cities[closest]) |
| 182 |
} |
| 183 |
|
| 184 |
get_furthest <- function(column) { |
| 185 |
furthest <- which.max(column[column != 0]) |
| 186 |
cities <- attr(column, "nms")[column != 0] |
| 187 |
toString(cities[furthest]) |
| 188 |
} |
| 189 |
``` |
| 190 |
|
| 191 |
This function, like all summary functions used by `skimr` has two notable |
| 192 |
features. |
| 193 |
|
| 194 |
* It accepts a vector as its single argument |
| 195 |
* it returns a scalar, or in R terminology, a vector of length 1. |
| 196 |
|
| 197 |
There are a lot of functions that fulfill these criteria: |
| 198 |
|
| 199 |
* Existing functions from base, stats, or other packages, |
| 200 |
* lambda's created using the Tidyverse-style syntax |
| 201 |
* custom functions that have been defined in the `skimr` package |
| 202 |
* custom functions that you have defined. |
| 203 |
|
| 204 |
Not fulfilling the two criteria can lead to some very confusing behavior within |
| 205 |
`skimr`. Beware! An example of this issue is the base `quantile()` function in |
| 206 |
default `skimr` percentiles are returned by using `quantile()` five |
| 207 |
times. In the case of these functions, there could be ties which would result in |
| 208 |
returning vectors that have length greater than 1. This is handled by collapsing |
| 209 |
all of the tied values into a single string. |
| 210 |
|
| 211 |
Notice, also, that in the case of distance data we may wish to exclude distances |
| 212 |
of 0, which indicate the distance from a place to itself. In finding the minimum |
| 213 |
our function looks only at the distance to other places. |
| 214 |
|
| 215 |
There are at least two ways that you might want to customize skimr handling of a |
| 216 |
special data type within a package or your own work. The first is to create a |
| 217 |
custom skimming function. |
| 218 |
|
| 219 |
````{r} |
| 220 |
skim_with_dist <- skim_with( |
| 221 |
distance = sfl( |
| 222 |
nearest = get_nearest, |
| 223 |
furthest = get_furthest |
| 224 |
) |
| 225 |
) |
| 226 |
|
| 227 |
skim_with_dist(UScitiesD) |
| 228 |
```` |
| 229 |
|
| 230 |
The example above creates a new *function*, and you can call that function on |
| 231 |
a specific column with `distance` data to get the appropriate summary |
| 232 |
statistics. The `skim_with` factory also uses the default skimrs for things |
| 233 |
like factors, characters, and numerics. Therefore our `skim_with_dist` is like |
| 234 |
the regular `skim` function with the added ability to summarize `distance` |
| 235 |
columns. |
| 236 |
|
| 237 |
While this works for any data type and you can also include it within any |
| 238 |
package (assuming your users load skimr), there is a second, even better, |
| 239 |
approach. To take full advantage of `skimr`, we'll dig a bit into its API. |
| 240 |
|
| 241 |
## Adding new methods |
| 242 |
|
| 243 |
`skimr` has a lookup mechanism, based on the function `get_skimmers()`, to |
| 244 |
find default summary functions for each class. This is based on the S3 class |
| 245 |
system. You can learn more about it in |
| 246 |
[*Advanced R*](https://adv-r.hadley.nz/s3.html). |
| 247 |
|
| 248 |
This requires that you add `skimr` to your list of dependencies. |
| 249 |
|
| 250 |
To export a new set of defaults for a data type, create a method for the generic |
| 251 |
function `get_skimmers`. Each of those methods returns an `sfl` (skimr |
| 252 |
function list) This is the same list-like data structure used in the |
| 253 |
`skim_with()` example above. But note! There is one key difference. When adding |
| 254 |
a generic we also want to identify the `skim_type` in the `sfl`. You will |
| 255 |
probably want to use `skimr::get_skimmers.distance()` but that will not work in |
| 256 |
a vignette. |
| 257 |
|
| 258 |
In a package you will want to export this. |
| 259 |
|
| 260 |
````{r} |
| 261 |
#' @importFrom skimr get_skimmers |
| 262 |
#' @export |
| 263 |
get_skimmers.distance <- function(column) { |
| 264 |
sfl( |
| 265 |
skim_type = "distance", |
| 266 |
nearest = get_nearest, |
| 267 |
furthest = get_furthest |
| 268 |
) |
| 269 |
} |
| 270 |
```` |
| 271 |
|
| 272 |
The same strategy follows for other data types. |
| 273 |
|
| 274 |
* Create a method |
| 275 |
* return an `sfl` |
| 276 |
* make sure that the `skim_type` is included. |
| 277 |
|
| 278 |
Users of your package should load `skimr` to get the `skim()` function (although |
| 279 |
you could import and reexport it). Once loaded, a call to |
| 280 |
`get_default_skimmer_names()` will return defaults for your data types as well! |
| 281 |
|
| 282 |
```{r} |
| 283 |
get_default_skimmer_names() |
| 284 |
``` |
| 285 |
|
| 286 |
They will then be able to use `skim()` directly. |
| 287 |
|
| 288 |
```{r} |
| 289 |
skim(UScitiesD) |
| 290 |
``` |
| 291 |
|
| 292 |
|
| 293 |
## Conclusion |
| 294 |
|
| 295 |
This is a very simple example. For some packages the custom statistics |
| 296 |
will likely be much more complex. The flexibility of `skimr` allows you to |
| 297 |
manage that. |
| 298 |
|
| 299 |
Thanks to Jakub Nowosad, Tiernan Martin, Edzer Pebesma, Michael Sumner, and |
| 300 |
Kyle Butts for inspiring and helping with the development of this code. |