diff --git a/DESCRIPTION b/DESCRIPTION index 4269d850..a29eade6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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, diff --git a/R/skim_print.R b/R/skim_print.R index 20271a8a..4078f6e4 100644 --- a/R/skim_print.R +++ b/R/skim_print.R @@ -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) @@ -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. diff --git a/tests/testthat/dplyr/arrange.txt b/tests/testthat/dplyr/arrange.txt new file mode 100644 index 00000000..e86cc1b3 --- /dev/null +++ b/tests/testthat/dplyr/arrange.txt @@ -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 ▇▁▇▅▃ diff --git a/tests/testthat/dplyr/filter-no-skim.txt b/tests/testthat/dplyr/filter-no-skim.txt new file mode 100644 index 00000000..82fb55c1 --- /dev/null +++ b/tests/testthat/dplyr/filter-no-skim.txt @@ -0,0 +1,4 @@ +# A tibble: 0 x 16 +# … with 16 variables: skim_type , skim_variable , missing , complete , +# n , ordered , n_unique , top_counts , mean , sd , p0 , +# p25 , p50 , p75 , p100 , hist diff --git a/tests/testthat/dplyr/filter-skim.txt b/tests/testthat/dplyr/filter-skim.txt new file mode 100644 index 00000000..0625fbce --- /dev/null +++ b/tests/testthat/dplyr/filter-skim.txt @@ -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 ▇▁▇▅▃ diff --git a/tests/testthat/dplyr/mutate-no-skim.txt b/tests/testthat/dplyr/mutate-no-skim.txt new file mode 100644 index 00000000..01275262 --- /dev/null +++ b/tests/testthat/dplyr/mutate-no-skim.txt @@ -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 +* +1 factor 0 150 150 FALSE 3 set: 50, … NA NA NA NA NA NA +2 numeric 0 150 150 NA NA 5.84 0.828 4.3 5.1 5.8 6.4 +3 numeric 0 150 150 NA NA 3.06 0.436 2 2.8 3 3.3 +4 numeric 0 150 150 NA NA 3.76 1.77 1 1.6 4.35 5.1 +5 numeric 0 150 150 NA NA 1.20 0.762 0.1 0.3 1.3 1.8 +# … with 2 more variables: p100 , hist diff --git a/tests/testthat/dplyr/mutate-skim.txt b/tests/testthat/dplyr/mutate-skim.txt new file mode 100644 index 00000000..ccc775b8 --- /dev/null +++ b/tests/testthat/dplyr/mutate-skim.txt @@ -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 diff --git a/tests/testthat/dplyr/select-no-skim.txt b/tests/testthat/dplyr/select-no-skim.txt new file mode 100644 index 00000000..dcf6d4da --- /dev/null +++ b/tests/testthat/dplyr/select-no-skim.txt @@ -0,0 +1,8 @@ +# A tibble: 5 x 1 + mean +* +1 NA +2 5.84 +3 3.06 +4 3.76 +5 1.20 diff --git a/tests/testthat/dplyr/select-skim.txt b/tests/testthat/dplyr/select-skim.txt new file mode 100644 index 00000000..d6831e35 --- /dev/null +++ b/tests/testthat/dplyr/select-skim.txt @@ -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 diff --git a/tests/testthat/dplyr/slice.txt b/tests/testthat/dplyr/slice.txt new file mode 100644 index 00000000..a1f07270 --- /dev/null +++ b/tests/testthat/dplyr/slice.txt @@ -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 ▁▆▇▂▁ diff --git a/tests/testthat/helper-expectations.R b/tests/testthat/helper-expectations.R index b2f51af2..5a3ad10e 100644 --- a/tests/testthat/helper-expectations.R +++ b/tests/testthat/helper-expectations.R @@ -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) +} diff --git a/tests/testthat/print/default.txt b/tests/testthat/print/default.txt new file mode 100644 index 00000000..d821b81a --- /dev/null +++ b/tests/testthat/print/default.txt @@ -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 ▇▁▇▅▃ diff --git a/tests/testthat/print/fallback.txt b/tests/testthat/print/fallback.txt new file mode 100644 index 00000000..dcf6d4da --- /dev/null +++ b/tests/testthat/print/fallback.txt @@ -0,0 +1,8 @@ +# A tibble: 5 x 1 + mean +* +1 NA +2 5.84 +3 3.06 +4 3.76 +5 1.20 diff --git a/tests/testthat/print/focus.txt b/tests/testthat/print/focus.txt new file mode 100644 index 00000000..f35d47b6 --- /dev/null +++ b/tests/testthat/print/focus.txt @@ -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 diff --git a/tests/testthat/print/groups.txt b/tests/testthat/print/groups.txt new file mode 100644 index 00000000..b9f0d289 --- /dev/null +++ b/tests/testthat/print/groups.txt @@ -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 + * + 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 ▂▇▆▅▇ diff --git a/tests/testthat/print/knit_print-skim_list.txt b/tests/testthat/print/knit_print-skim_list.txt new file mode 100644 index 00000000..40a48e71 --- /dev/null +++ b/tests/testthat/print/knit_print-skim_list.txt @@ -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|▇▁▇▅▃ | + + diff --git a/tests/testthat/print/knit_print-summary.txt b/tests/testthat/print/knit_print-summary.txt new file mode 100644 index 00000000..885478d0 --- /dev/null +++ b/tests/testthat/print/knit_print-summary.txt @@ -0,0 +1,12 @@ +| | | +|:----------------------|:----| +|Name |iris | +|Number of rows |150 | +|Number of columns |5 | +| | | +|Column type frequency: | | +|factor |1 | +|numeric |4 | +| | | +|Group variables |None | +| | | diff --git a/tests/testthat/print/knit_print-suppressed.txt b/tests/testthat/print/knit_print-suppressed.txt new file mode 100644 index 00000000..40a48e71 --- /dev/null +++ b/tests/testthat/print/knit_print-suppressed.txt @@ -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|▇▁▇▅▃ | + + diff --git a/tests/testthat/print/knit_print-yank.txt b/tests/testthat/print/knit_print-yank.txt new file mode 100644 index 00000000..16e08e29 --- /dev/null +++ b/tests/testthat/print/knit_print-yank.txt @@ -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 | + + + diff --git a/tests/testthat/print/knit_print.txt b/tests/testthat/print/knit_print.txt new file mode 100644 index 00000000..f06b618e --- /dev/null +++ b/tests/testthat/print/knit_print.txt @@ -0,0 +1,32 @@ + +| | | +|:----------------------|:----| +|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 | +|:-------------|-------:|--------:|---:|:-------|--------:|:-------------------------| +|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|▇▁▇▅▃ | + + diff --git a/tests/testthat/print/list.txt b/tests/testthat/print/list.txt new file mode 100644 index 00000000..ee00b6a7 --- /dev/null +++ b/tests/testthat/print/list.txt @@ -0,0 +1,13 @@ +$factor +── 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 + +$numeric +── 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 ▇▁▇▅▃ + diff --git a/tests/testthat/print/no-hist.txt b/tests/testthat/print/no-hist.txt new file mode 100644 index 00000000..87953529 --- /dev/null +++ b/tests/testthat/print/no-hist.txt @@ -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 +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 diff --git a/tests/testthat/print/smaller.txt b/tests/testthat/print/smaller.txt new file mode 100644 index 00000000..d821b81a --- /dev/null +++ b/tests/testthat/print/smaller.txt @@ -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 ▇▁▇▅▃ diff --git a/tests/testthat/test-dplyr.R b/tests/testthat/test-dplyr.R index 8bce33be..597bcbd8 100644 --- a/tests/testthat/test-dplyr.R +++ b/tests/testthat/test-dplyr.R @@ -4,38 +4,34 @@ skimmed_iris <- skim(iris) test_that("dplyr::filter works as expected", { input <- dplyr::filter(skimmed_iris, skim_type == "numeric") - expect_output(print(input), "── Data Summary ────────────────────────\\n") + expect_print_matches_file(input, "dplyr/filter-skim.txt") no_rows <- dplyr::filter(skimmed_iris, skim_type == "no_type") - expect_output(print(no_rows), "# A tibble: 0 x 16") + expect_print_matches_file(no_rows, "dplyr/filter-no-skim.txt") }) test_that("dplyr::select works as expected", { with_type <- dplyr::select(skimmed_iris, skim_type, skim_variable) - expect_output(print(with_type), "── Data Summary ────────────────────────\\n") + expect_print_matches_file(with_type, "dplyr/select-skim.txt") without_type <- dplyr::select(skimmed_iris, mean) - expect_output(print(without_type), "# A tibble") + expect_print_matches_file(without_type, "dplyr/select-no-skim.txt") }) test_that("dplyr::mutate works as expected", { input <- dplyr::mutate(skimmed_iris, mean2 = mean^2) - expect_output(print(input), "── Data Summary ────────────────────────\\n") + expect_print_matches_file(input, "dplyr/mutate-skim.txt") no_variable <- dplyr::mutate(skimmed_iris, skim_variable = NULL) - expect_output(print(no_variable), "# A tibble: 5 x 15") + expect_print_matches_file(no_variable, "dplyr/mutate-no-skim.txt") }) test_that("dplyr::slice works as expected", { input <- dplyr::slice(skimmed_iris, 1:3) - expect_output(print(input), "── Data Summary ────────────────────────\\n") + expect_print_matches_file(input, "dplyr/slice.txt") }) test_that("dplyr::arrange works as expected", { input <- dplyr::arrange(skimmed_iris, desc(mean)) - expect_output(print(input), "── Data Summary ────────────────────────\\n") - expect_output(print(input), "1 Sepal.Length") - expect_output(print(input), "2 Petal.Length") - expect_output(print(input), "3 Sepal.Width") - expect_output(print(input), "4 Petal.Width") + expect_print_matches_file(input, "dplyr/arrange.txt") }) diff --git a/tests/testthat/test-dplyr.R.orig b/tests/testthat/test-dplyr.R.orig deleted file mode 100644 index 59cd5664..00000000 --- a/tests/testthat/test-dplyr.R.orig +++ /dev/null @@ -1,49 +0,0 @@ -context("Using dplyr verbs on skim objects works as expected") - -skimmed_iris <- skim(iris) - -test_that("dplyr::filter works as expected", { - input <- dplyr::filter(skimmed_iris, skim_type == "numeric") -<<<<<<< HEAD - expect_output(print(input), "Skim summary statistics") -======= - expect_output(print(input), "Data summary \\n") ->>>>>>> v2 - - no_rows <- dplyr::filter(skimmed_iris, skim_type == "no_type") - expect_output(print(no_rows), "# A tibble: 0 x 16") -}) - -test_that("dplyr::select works as expected", { - with_type <- dplyr::select(skimmed_iris, skim_type, skim_variable) - expect_output(print(with_type), "Data summary \\n") - - without_type <- dplyr::select(skimmed_iris, mean) - expect_output(print(without_type), "# A tibble") -}) - -test_that("dplyr::mutate works as expected", { - input <- dplyr::mutate(skimmed_iris, mean2 = mean^2) -<<<<<<< HEAD - expect_output(print(input), "Skim summary statistics") -======= - expect_output(print(input), "Data summary \\n") ->>>>>>> v2 - - no_variable <- dplyr::mutate(skimmed_iris, skim_variable = NULL) - expect_output(print(no_variable), "# A tibble: 5 x 15") -}) - -test_that("dplyr::slice works as expected", { - input <- dplyr::slice(skimmed_iris, 1:3) - expect_output(print(input), "Data summary \\n") -}) - -test_that("dplyr::arrange works as expected", { - input <- dplyr::arrange(skimmed_iris, desc(mean)) - expect_output(print(input), "Data summary \\n") - expect_output(print(input), "1 Sepal.Length") - expect_output(print(input), "2 Petal.Length") - expect_output(print(input), "3 Sepal.Width") - expect_output(print(input), "4 Petal.Width") -}) diff --git a/tests/testthat/test-reshape.R.orig b/tests/testthat/test-reshape.R.orig deleted file mode 100644 index 873fd5a2..00000000 --- a/tests/testthat/test-reshape.R.orig +++ /dev/null @@ -1,127 +0,0 @@ -context("Reshaping a skim_df") - -test_that("You can parition a skim_df", { - skimmed <- skim(iris) - input <- partition(skimmed) - expect_is(input, "skim_list") - expect_length(input, 2) - expect_named(input, c("factor", "numeric")) - attrs <- attributes(input) - expect_equal(attrs$data_rows, 150) - expect_equal(attrs$data_cols, 5) - expect_identical(attrs$df_name, "`iris`") - expect_identical( - attrs$skimmers_used, - list( - numeric = c( - "missing", "complete", "n", "mean", "sd", "p0", - "p25", "p50", "p75", "p100", "hist" - ), - factor = c( - "missing", "complete", "n", "ordered", - "n_unique", "top_counts" - ) - ) - ) - - # Subtables - expect_is(input$factor, c("one_skim_df", "tbl_df", "tbl", "data.frame")) - expect_n_rows(input$factor, 1) - expect_n_columns(input$factor, 7) - expect_named(input$factor, c( - "skim_variable", "missing", "complete", "n", - "ordered", "n_unique", "top_counts" - )) - - expect_is(input$numeric, c("one_skim_df", "tbl_df", "tbl", "data.frame")) - expect_n_rows(input$numeric, 4) - expect_n_columns(input$numeric, 12) - expect_named(input$numeric, c( - "skim_variable", "missing", "complete", "n", "mean", - "sd", "p0", "p25", "p50", "p75", "p100", - "hist" - )) -}) - -test_that("Partitioning works in a round trip", { - skimmed <- skim(iris) - partitioned <- partition(skimmed) - input <- bind(partitioned) - expect_equal(input, skimmed) -}) - -test_that("You can yank a subtable from a skim_df", { - skimmed <- skim(iris) - input <- yank(skimmed, "numeric") - expect_is(input, c("one_skim_df", "tbl_df", "tbl", "data.frame")) - expect_n_rows(input, 4) - expect_n_columns(input, 12) - expect_named(input, c( - "skim_variable", "missing", "complete", "n", "mean", - "sd", "p0", "p25", "p50", "p75", "p100", - "hist" - )) -}) - -test_that("Partition is safe if some skimmers are missing", { - skimmed <- skim(iris) - reduced <- dplyr::select(skimmed, skim_variable, skim_type, missing) - partitioned <- partition(reduced) - expect_length(partitioned, 2) - expect_named(partitioned, c("factor", "numeric")) - expect_named(partitioned$numeric, c("skim_variable", "missing")) -}) - -test_that("Partition handles new columns", { - skimmed <- skim(iris) - expanded <- dplyr::mutate(skimmed, mean2 = mean^2, complete2 = complete^2) - partitioned <- partition(expanded) - expect_named(partitioned$numeric, c( - "skim_variable", "missing", "complete", "n", "mean", - "sd", "p0", "p25", "p50", "p75", "p100", - "hist", "mean2", "complete2" - )) - - expect_named(partitioned$factor, c( - "skim_variable", "missing", "complete", "n", - "ordered", "n_unique", "top_counts", "complete2" - )) -}) - -test_that("focus() matches select(data, skim_type, skim_variable, ...)", { - skimmed <- skim(iris) - expected <- dplyr::select(skimmed, skim_type, skim_variable, missing) - expect_identical(focus(skimmed, missing), expected) -}) - -test_that("focus() does not allow dropping skim metadata columns", { - skimmed <- skim(iris) -<<<<<<< HEAD - expect_error(focus(skimmed, -skim_variable), "Cannot drop") - expect_error(focus(skimmed, -skim_type), "Cannot drop") -======= - expect_error(focus(skimmed, -skim_variable), "Cannot drop 'skim_variable' or 'skim_type' columns") - expect_error(focus(skimmed, -skim_type), "Cannot drop 'skim_variable' or 'skim_type' columns") ->>>>>>> v2 -}) - -test_that("skim_to_wide() returns a deprecation warning", { - expect_warning(skim_to_wide(iris)) -}) - -test_that("skim_to_list() returns a deprecation warning", { - expect_warning(skim_to_list(iris)) -}) - -test_that("to_long() returns a long tidy data frame with 4 columns", { - skimmed_long <- to_long(iris) - # Statistics from the skim_df with values of NA are not included - expect_equal(nrow(skimmed_long), 50) - expect_equal( - names(skimmed_long), - c("skim_type", "skim_variable", "stat", "formatted") - ) - expect_equal(length(unique(skimmed_long$stat)), 14) - expect_equal(length(unique(skimmed_long$skim_type)), 2) - expect_equal(length(unique(skimmed_long$skim_variable)), 5) -}) diff --git a/tests/testthat/test-skim_print.R b/tests/testthat/test-skim_print.R index 1ae70739..2101c9a9 100644 --- a/tests/testthat/test-skim_print.R +++ b/tests/testthat/test-skim_print.R @@ -2,85 +2,57 @@ context("Print a skim_df object") test_that("Skim prints a header for the entire output and each type", { input <- skim(iris) - expect_output(print(input), "── Data Summary ────────────────────────") - expect_output(print(input), " Value") - expect_output(print(input), "Name iris") - expect_output(print(input), "Number of rows 150") - expect_output(print(input), "Number of columns 5") - expect_output(print(input), "Column type frequency: ") - expect_output(print(input), " factor 1") - expect_output(print(input), " numeric 4") - expect_output(print(input), "Group variables None") - expect_output(print(input), "── Variable type: factor ────────────────") - expect_output(print(input), "── Variable type: numeric ────────────────") + expect_print_matches_file(input, "print/default.txt") + + input$hist <- NULL + expect_print_matches_file(input, "print/no-hist.txt", skip_on_windows = FALSE) }) test_that("Skim prints a special header for grouped data frames", { input <- skim(dplyr::group_by(iris, Species)) - expect_output(print(input), "── Data Summary ────────────────────────") - expect_output(print(input), "Name dplyr::group_by(iris, Spe...", fixed =TRUE) - expect_output(print(input), "Number of rows 150") - expect_output(print(input), "Number of columns 5") - expect_output(print(input), "Group variables Species") + expect_print_matches_file(input, "print/groups.txt") }) test_that("Skim lists print as expected", { skimmed <- skim(iris) input <- partition(skimmed) - expect_output(print(input), "\\$factor") - expect_output(print(input), "── Variable type: factor ────────────────") - expect_output(print(input), "\\$numeric") - expect_output(print(input), "── Variable type: numeric ───────────────") + expect_print_matches_file(input, "print/list.txt") }) -test_that("knit_print produced expected results", { +test_that("knit_print produces expected results", { skimmed <- skim(iris) input <- knit_print(skimmed) expect_is(input, "knit_asis") expect_length(input, 1) - expect_match( - input, - "|skim_variable | missing| complete| n|ordered | n_unique|top_counts" - ) + expect_matches_file(input, "print/knit_print.txt") }) test_that("knit_print works with skim summaries", { skimmed <- skim(iris) summarized <- summary(skimmed) - multi_line <- capture.output(knitr::knit_print(summarized)) - input <- paste(multi_line, collapse = "") - # expect_match(input, "Data summary ", fixed = TRUE) - # expect_match(input, "df_name") - expect_output(print(input), "|Name |`iris` |") - expect_output(print(input), "|Number of rows |150 |") - expect_output(print(input), "|Number of columns |5 |") - expect_output(print(input), "| | |") - expect_match(input, "|Column type frequency: | |", fixed = TRUE) - expect_match(input, "|factor |1 |") - expect_match(input, "|numeric |4 |") + input <- knitr::knit_print(summarized) + expect_matches_file(input, "print/knit_print-summary.txt") }) test_that("Summaries can be suppressed within knitr", { skimmed <- skim(iris) options <- list(skimr_include_summary = FALSE) - input <- knit_print(skimmed, options = options) - expect_false(grepl("── Data Summary ────────────────────────\\n", input)) + input <- knitr::knit_print(skimmed, options = options) + expect_matches_file(input, "print/knit_print-suppressed.txt") }) test_that("Skim lists have a separate knit_print method", { skimmed <- skim(iris) skim_list <- partition(skimmed) input <- knit_print(skim_list) - expect_match(input, "\n\n\n**Variable type: factor**\n\n", fixed = TRUE) - expect_match(input, "\n\n\n**Variable type: numeric**\n\n", fixed = TRUE) + expect_matches_file(input, "print/knit_print-skim_list.txt") }) test_that("You can yank a type from a skim_df and call knit_print", { skimmed <- skim(iris) skim_one <- yank(skimmed, "factor") input <- knit_print(skim_one) - expect_match(input, "\n\n**Variable type: factor**\n\n", fixed = TRUE) - expect_false(grepl("\n\n**Variable type: numeric**\n\n", input, fixed = TRUE)) + expect_matches_file(input, "print/knit_print-yank.txt") }) test_that("make_utf8 produces the correct result ", { @@ -92,16 +64,33 @@ test_that("make_utf8 produces the correct result ", { test_that("Skim falls back to tibble::print.tbl() appropriately", { input <- skim(iris) mean_only <- dplyr::select(input, mean) - expect_output(print(mean_only), "# A tibble: 5 x 1") + expect_print_matches_file(mean_only, "print/fallback.txt") }) test_that("Print focused objects appropriately", { skimmed <- skim(iris) input <- focus(skimmed, missing) - expect_output(print(input), "Name iris") - expect_output(print(input), "Number of rows 150") - expect_output(print(input), "Number of columns 5") - expect_output(print(input), "Column type frequency: ") - expect_output(print(input), " factor 1") - expect_output(print(input), " numeric 4") + expect_print_matches_file(input, "print/focus.txt") +}) + +test_that("Metadata is stripped from smaller consoles", { + withr::with_options(list(width = 50), { + skimmed <- skim(iris) + expect_print_matches_file(skimmed, "print/smaller.txt") + }) +}) + +test_that("Crayon is supported", { + withr::with_options(list(crayon.enabled = TRUE), { + with_mock( + .env = "skimr", + render_skim_body = function(...) paste0(..., sep = "\n", collapse = "\n"), + { + skimmed <- skim(iris) + numeric <- yank(skimmed, "numeric") + rendered <- print(numeric) + } + ) + expect_match(rendered, "\\\033") + }) })