Skip to content

Commit

Permalink
Update printing handling of meta data from tibbles and improve print …
Browse files Browse the repository at this point in the history
…test structure.

V2 print
  • Loading branch information
elinw authored Mar 24, 2019
2 parents fd0bb47 + 1843865 commit c4d573c
Show file tree
Hide file tree
Showing 27 changed files with 451 additions and 246 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ Imports:
knitr (>= 1.2),
tibble (>= 2.0.0),
tidyr (>= 0.7),
tidyselect (>= 0.2.5)
tidyselect (>= 0.2.5),
crayon
Suggests:
extrafont,
rmarkdown,
Expand Down
25 changes: 17 additions & 8 deletions R/skim_print.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ NULL
print.skim_df <- function(x, include_summary = TRUE, n = Inf, width = Inf,
n_extra = NULL, ...) {
if (is_skim_df(x)) {
if (include_summary) {
print(summary(x))
}

if (include_summary) print(summary(x))
by_type <- partition(x)
purrr::imap(by_type, print, n, width, n_extra)
invisible(NULL)
Expand All @@ -35,10 +32,22 @@ print.skim_df <- function(x, include_summary = TRUE, n = Inf, width = Inf,
#' @export
print.one_skim_df <- function(x, n = Inf, width = Inf, n_extra = NULL, ...) {
variable_type <- paste("Variable type:", attr(x, "skim_type"))
with_line <- cli::rule(line = 1, left = variable_type)
print(with_line)
out <- format(x, n = n, width = width, n_extra, ...)
cat(out[c(-1, -3)], sep = "\n")
top_line <- cli::rule(line = 1, left = variable_type)
out <- format(x, n = n, width = width, n_extra = n_extra, ...)
metadata <- grab_tibble_metadata(out)
render_skim_body(top_line, out, metadata)
}

grab_tibble_metadata <- function(x) {
if (crayon::has_color()) {
grep("^\\\033\\[38;5;\\d{3}m[#\\*]", x)
} else {
grep("^[#\\*]", x)
}
}

render_skim_body <- function(top_line, out, metadata) {
cat(top_line, out[-metadata], sep = "\n")
}

#' @describeIn print Print a `skim_list`, a list of `skim_df` objects.
Expand Down
21 changes: 21 additions & 0 deletions tests/testthat/dplyr/arrange.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
── Data Summary ────────────────────────
Value
Name iris
Number of rows 150
Number of columns 5

Column type frequency:
factor 1
numeric 4

Group variables None

── Variable type: factor ───────────────────────────────────────────────────────────────────────────
skim_variable missing complete n ordered n_unique top_counts
1 Species 0 150 150 FALSE 3 set: 50, ver: 50, vir: 50
── Variable type: numeric ──────────────────────────────────────────────────────────────────────────
skim_variable missing complete n mean sd p0 p25 p50 p75 p100 hist
1 Sepal.Length 0 150 150 5.84 0.828 4.3 5.1 5.8 6.4 7.9 ▆▇▇▅▂
2 Petal.Length 0 150 150 3.76 1.77 1 1.6 4.35 5.1 6.9 ▇▁▆▇▂
3 Sepal.Width 0 150 150 3.06 0.436 2 2.8 3 3.3 4.4 ▁▆▇▂▁
4 Petal.Width 0 150 150 1.20 0.762 0.1 0.3 1.3 1.8 2.5 ▇▁▇▅▃
4 changes: 4 additions & 0 deletions tests/testthat/dplyr/filter-no-skim.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# A tibble: 0 x 16
# … with 16 variables: skim_type <chr>, skim_variable <chr>, missing <int>, complete <int>,
# n <int>, ordered <lgl>, n_unique <int>, top_counts <chr>, mean <dbl>, sd <dbl>, p0 <dbl>,
# p25 <dbl>, p50 <dbl>, p75 <dbl>, p100 <dbl>, hist <chr>
17 changes: 17 additions & 0 deletions tests/testthat/dplyr/filter-skim.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
── Data Summary ────────────────────────
Value
Name iris
Number of rows 150
Number of columns 5

Column type frequency:
numeric 4

Group variables None

── Variable type: numeric ──────────────────────────────────────────────────────────────────────────
skim_variable missing complete n mean sd p0 p25 p50 p75 p100 hist
1 Sepal.Length 0 150 150 5.84 0.828 4.3 5.1 5.8 6.4 7.9 ▆▇▇▅▂
2 Sepal.Width 0 150 150 3.06 0.436 2 2.8 3 3.3 4.4 ▁▆▇▂▁
3 Petal.Length 0 150 150 3.76 1.77 1 1.6 4.35 5.1 6.9 ▇▁▆▇▂
4 Petal.Width 0 150 150 1.20 0.762 0.1 0.3 1.3 1.8 2.5 ▇▁▇▅▃
9 changes: 9 additions & 0 deletions tests/testthat/dplyr/mutate-no-skim.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# A tibble: 5 x 15
skim_type missing complete n ordered n_unique top_counts mean sd p0 p25 p50 p75
* <chr> <int> <int> <int> <lgl> <int> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 factor 0 150 150 FALSE 3 set: 50, … NA NA NA NA NA NA
2 numeric 0 150 150 NA NA <NA> 5.84 0.828 4.3 5.1 5.8 6.4
3 numeric 0 150 150 NA NA <NA> 3.06 0.436 2 2.8 3 3.3
4 numeric 0 150 150 NA NA <NA> 3.76 1.77 1 1.6 4.35 5.1
5 numeric 0 150 150 NA NA <NA> 1.20 0.762 0.1 0.3 1.3 1.8
# … with 2 more variables: p100 <dbl>, hist <chr>
21 changes: 21 additions & 0 deletions tests/testthat/dplyr/mutate-skim.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
── Data Summary ────────────────────────
Value
Name iris
Number of rows 150
Number of columns 5

Column type frequency:
factor 1
numeric 4

Group variables None

── Variable type: factor ───────────────────────────────────────────────────────────────────────────
skim_variable missing complete n ordered n_unique top_counts
1 Species 0 150 150 FALSE 3 set: 50, ver: 50, vir: 50
── Variable type: numeric ──────────────────────────────────────────────────────────────────────────
skim_variable missing complete n mean sd p0 p25 p50 p75 p100 hist mean2
1 Sepal.Length 0 150 150 5.84 0.828 4.3 5.1 5.8 6.4 7.9 ▆▇▇▅▂ 34.1
2 Sepal.Width 0 150 150 3.06 0.436 2 2.8 3 3.3 4.4 ▁▆▇▂▁ 9.35
3 Petal.Length 0 150 150 3.76 1.77 1 1.6 4.35 5.1 6.9 ▇▁▆▇▂ 14.1
4 Petal.Width 0 150 150 1.20 0.762 0.1 0.3 1.3 1.8 2.5 ▇▁▇▅▃ 1.44
8 changes: 8 additions & 0 deletions tests/testthat/dplyr/select-no-skim.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# A tibble: 5 x 1
mean
* <dbl>
1 NA
2 5.84
3 3.06
4 3.76
5 1.20
21 changes: 21 additions & 0 deletions tests/testthat/dplyr/select-skim.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
── Data Summary ────────────────────────
Value
Name iris
Number of rows 150
Number of columns 5

Column type frequency:
factor 1
numeric 4

Group variables None

── Variable type: factor ───────────────────────────────────────────────────────────────────────────
skim_variable
1 Species
── Variable type: numeric ──────────────────────────────────────────────────────────────────────────
skim_variable
1 Sepal.Length
2 Sepal.Width
3 Petal.Length
4 Petal.Width
19 changes: 19 additions & 0 deletions tests/testthat/dplyr/slice.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
── Data Summary ────────────────────────
Value
Name iris
Number of rows 150
Number of columns 5

Column type frequency:
factor 1
numeric 2

Group variables None

── Variable type: factor ───────────────────────────────────────────────────────────────────────────
skim_variable missing complete n ordered n_unique top_counts
1 Species 0 150 150 FALSE 3 set: 50, ver: 50, vir: 50
── Variable type: numeric ──────────────────────────────────────────────────────────────────────────
skim_variable missing complete n mean sd p0 p25 p50 p75 p100 hist
1 Sepal.Length 0 150 150 5.84 0.828 4.3 5.1 5.8 6.4 7.9 ▆▇▇▅▂
2 Sepal.Width 0 150 150 3.06 0.436 2 2.8 3 3.3 4.4 ▁▆▇▂▁
41 changes: 41 additions & 0 deletions tests/testthat/helper-expectations.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,44 @@ expect_NA <- function(object) {
expect(is.na(act$val), sprintf("%s is not NA", act$lab))
invisible(act$val)
}

expect_print_matches_file <- function(object, filename, skip_on_windows = TRUE) {
if (skip_on_windows) testthat::skip_on_os("windows")
withr::with_options(list(crayon.enabled = FALSE), {
testthat::expect_known_output(
print(object),
filename,
update = TRUE,
width = 100
)
})
}

expect_matches_file <- function(object, file, update = TRUE,
skip_on_windows = TRUE, ...) {
if (skip_on_windows) testthat::skip_on_os("windows")
act <- testthat::quasi_label(rlang::enquo(object), NULL)

if (!file.exists(file)) {
warning("Creating reference value", call. = FALSE)
writeLines(object, file)
testthat::succeed()
} else {
ref_val <- paste0(readLines(file), collapse = "\n")
comp <- testthat::compare(as.character(act$val), ref_val, ...)
if (update && !comp$equal) {
writeLines(act$val, file)
}

expect(
comp$equal,
sprintf(
"%s has changed from known value recorded in %s.\n%s",
act$lab, encodeString(file, quote = "'"), comp$message
),
info = NULL
)
}

invisible(act$value)
}
21 changes: 21 additions & 0 deletions tests/testthat/print/default.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
── Data Summary ────────────────────────
Value
Name iris
Number of rows 150
Number of columns 5

Column type frequency:
factor 1
numeric 4

Group variables None

── Variable type: factor ───────────────────────────────────────────────────────────────────────────
skim_variable missing complete n ordered n_unique top_counts
1 Species 0 150 150 FALSE 3 set: 50, ver: 50, vir: 50
── Variable type: numeric ──────────────────────────────────────────────────────────────────────────
skim_variable missing complete n mean sd p0 p25 p50 p75 p100 hist
1 Sepal.Length 0 150 150 5.84 0.828 4.3 5.1 5.8 6.4 7.9 ▆▇▇▅▂
2 Sepal.Width 0 150 150 3.06 0.436 2 2.8 3 3.3 4.4 ▁▆▇▂▁
3 Petal.Length 0 150 150 3.76 1.77 1 1.6 4.35 5.1 6.9 ▇▁▆▇▂
4 Petal.Width 0 150 150 1.20 0.762 0.1 0.3 1.3 1.8 2.5 ▇▁▇▅▃
8 changes: 8 additions & 0 deletions tests/testthat/print/fallback.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# A tibble: 5 x 1
mean
* <dbl>
1 NA
2 5.84
3 3.06
4 3.76
5 1.20
21 changes: 21 additions & 0 deletions tests/testthat/print/focus.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
── Data Summary ────────────────────────
Value
Name iris
Number of rows 150
Number of columns 5

Column type frequency:
factor 1
numeric 4

Group variables None

── Variable type: factor ───────────────────────────────────────────────────────────────────────────
skim_variable missing
1 Species 0
── Variable type: numeric ──────────────────────────────────────────────────────────────────────────
skim_variable missing
1 Sepal.Length 0
2 Sepal.Width 0
3 Petal.Length 0
4 Petal.Width 0
26 changes: 26 additions & 0 deletions tests/testthat/print/groups.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
── Data Summary ────────────────────────
Value
Name dplyr::group_by(iris, Spe...
Number of rows 150
Number of columns 5

Column type frequency:
numeric 4

Group variables Species

── Variable type: numeric ──────────────────────────────────────────────────────────────────────────
skim_variable Species missing complete n mean sd p0 p25 p50 p75 p100 hist
* <chr> <fct> <int> <int> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
1 Sepal.Length setosa 0 50 50 5.01 0.352 4.3 4.8 5 5.2 5.8 ▃▃▇▅▁
2 Sepal.Length versicolor 0 50 50 5.94 0.516 4.9 5.6 5.9 6.3 7 ▂▇▆▃▃
3 Sepal.Length virginica 0 50 50 6.59 0.636 4.9 6.22 6.5 6.9 7.9 ▁▃▇▃▂
4 Sepal.Width setosa 0 50 50 3.43 0.379 2.3 3.2 3.4 3.68 4.4 ▁▃▇▅▂
5 Sepal.Width versicolor 0 50 50 2.77 0.314 2 2.52 2.8 3 3.4 ▁▅▆▇▂
6 Sepal.Width virginica 0 50 50 2.97 0.322 2.2 2.8 3 3.18 3.8 ▂▆▇▅▁
7 Petal.Length setosa 0 50 50 1.46 0.174 1 1.4 1.5 1.58 1.9 ▁▃▇▃▁
8 Petal.Length versicolor 0 50 50 4.26 0.470 3 4 4.35 4.6 5.1 ▂▂▇▇▆
9 Petal.Length virginica 0 50 50 5.55 0.552 4.5 5.1 5.55 5.88 6.9 ▃▇▇▃▂
10 Petal.Width setosa 0 50 50 0.246 0.105 0.1 0.2 0.2 0.3 0.6 ▇▂▂▁▁
11 Petal.Width versicolor 0 50 50 1.33 0.198 1 1.2 1.3 1.5 1.8 ▅▇▃▆▁
12 Petal.Width virginica 0 50 50 2.03 0.275 1.4 1.8 2 2.3 2.5 ▂▇▆▅▇
20 changes: 20 additions & 0 deletions tests/testthat/print/knit_print-skim_list.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@



**Variable type: factor**

|skim_variable | missing| complete| n|ordered | n_unique|top_counts |
|:-------------|-------:|--------:|---:|:-------|--------:|:-------------------------|
|Species | 0| 150| 150|FALSE | 3|set: 50, ver: 50, vir: 50 |


**Variable type: numeric**

|skim_variable | missing| complete| n| mean| sd| p0| p25| p50| p75| p100|hist |
|:-------------|-------:|--------:|---:|----:|----:|---:|---:|----:|---:|----:|:-----|
|Sepal.Length | 0| 150| 150| 5.84| 0.83| 4.3| 5.1| 5.80| 6.4| 7.9|▆▇▇▅▂ |
|Sepal.Width | 0| 150| 150| 3.06| 0.44| 2.0| 2.8| 3.00| 3.3| 4.4|▁▆▇▂▁ |
|Petal.Length | 0| 150| 150| 3.76| 1.77| 1.0| 1.6| 4.35| 5.1| 6.9|▇▁▆▇▂ |
|Petal.Width | 0| 150| 150| 1.20| 0.76| 0.1| 0.3| 1.30| 1.8| 2.5|▇▁▇▅▃ |


12 changes: 12 additions & 0 deletions tests/testthat/print/knit_print-summary.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
| | |
|:----------------------|:----|
|Name |iris |
|Number of rows |150 |
|Number of columns |5 |
| | |
|Column type frequency: | |
|factor |1 |
|numeric |4 |
| | |
|Group variables |None |
| | |
20 changes: 20 additions & 0 deletions tests/testthat/print/knit_print-suppressed.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@



**Variable type: factor**

|skim_variable | missing| complete| n|ordered | n_unique|top_counts |
|:-------------|-------:|--------:|---:|:-------|--------:|:-------------------------|
|Species | 0| 150| 150|FALSE | 3|set: 50, ver: 50, vir: 50 |


**Variable type: numeric**

|skim_variable | missing| complete| n| mean| sd| p0| p25| p50| p75| p100|hist |
|:-------------|-------:|--------:|---:|----:|----:|---:|---:|----:|---:|----:|:-----|
|Sepal.Length | 0| 150| 150| 5.84| 0.83| 4.3| 5.1| 5.80| 6.4| 7.9|▆▇▇▅▂ |
|Sepal.Width | 0| 150| 150| 3.06| 0.44| 2.0| 2.8| 3.00| 3.3| 4.4|▁▆▇▂▁ |
|Petal.Length | 0| 150| 150| 3.76| 1.77| 1.0| 1.6| 4.35| 5.1| 6.9|▇▁▆▇▂ |
|Petal.Width | 0| 150| 150| 1.20| 0.76| 0.1| 0.3| 1.30| 1.8| 2.5|▇▁▇▅▃ |


10 changes: 10 additions & 0 deletions tests/testthat/print/knit_print-yank.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@


**Variable type: factor**

|skim_variable | missing| complete| n|ordered | n_unique|top_counts |
|:-------------|-------:|--------:|---:|:-------|--------:|:-------------------------|
|Species | 0| 150| 150|FALSE | 3|set: 50, ver: 50, vir: 50 |



Loading

0 comments on commit c4d573c

Please sign in to comment.