From b723df21b3644975bc6e46c44115d8c7b2826f57 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 4 Nov 2024 22:59:39 +0100 Subject: [PATCH] Fix snapshot tests (#560) * Fix snapshot tests Fixes #559 * update * pass ... to export_table * update tests * pass ... * update tests * docs * Trigger CI * lintr --- DESCRIPTION | 2 +- R/data_codebook.R | 6 +- R/data_tabulate.R | 9 +- R/data_xtabulate.R | 3 +- R/describe_distribution.R | 2 +- man/data_codebook.Rd | 3 +- man/data_tabulate.Rd | 3 +- tests/testthat/_snaps/data_codebook.md | 264 ++++++++++++++++++++++++- tests/testthat/test-data_codebook.R | 32 +-- 9 files changed, 299 insertions(+), 25 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 8127a2e79..a680cfd02 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: datawizard Title: Easy Data Wrangling and Statistical Transformations -Version: 0.13.0.9 +Version: 0.13.0.11 Authors@R: c( person("Indrajeet", "Patil", , "patilindrajeet.science@gmail.com", role = "aut", comment = c(ORCID = "0000-0003-1995-6531")), diff --git a/R/data_codebook.R b/R/data_codebook.R index 71e2e5828..5dffbf9c2 100644 --- a/R/data_codebook.R +++ b/R/data_codebook.R @@ -33,7 +33,8 @@ #' #' @note There are methods to `print()` the data frame in a nicer output, as #' well methods for printing in markdown or HTML format (`print_md()` and -#' `print_html()`). +#' `print_html()`). The `print()` method for text outputs passes arguments in +#' `...` to [`insight::export_table()`]. #' #' @examples #' data(iris) @@ -369,7 +370,8 @@ print.data_codebook <- function(x, ...) { title = caption, empty_line = "-", cross = "+", - align = .get_codebook_align(x) + align = .get_codebook_align(x), + ... ) ) } diff --git a/R/data_tabulate.R b/R/data_tabulate.R index e94fc5d55..621fabbb1 100644 --- a/R/data_tabulate.R +++ b/R/data_tabulate.R @@ -48,7 +48,8 @@ #' @note #' There are `print_html()` and `print_md()` methods available for printing #' frequency or crosstables in HTML and markdown format, e.g. -#' `print_html(data_tabulate(x))`. +#' `print_html(data_tabulate(x))`. The `print()` method for text outputs passes +#' arguments in `...` to [`insight::export_table()`]. #' #' @return A data frame, or a list of data frames, with one frequency table #' as data frame per variable. @@ -522,7 +523,8 @@ print.datawizard_table <- function(x, big_mark = NULL, ...) { cat(insight::export_table( format(x, big_mark = big_mark, ...), cross = "+", - missing = "" + missing = "", + ... )) invisible(x) } @@ -621,7 +623,8 @@ print.datawizard_tables <- function(x, big_mark = NULL, ...) { out, missing = "", cross = "+", - empty_line = "-" + empty_line = "-", + ... )) } } diff --git a/R/data_xtabulate.R b/R/data_xtabulate.R index 08be1eeca..c9595eccf 100644 --- a/R/data_xtabulate.R +++ b/R/data_xtabulate.R @@ -198,7 +198,8 @@ print.datawizard_crosstab <- function(x, big_mark = NULL, ...) { cross = "+", missing = "", caption = caption, - empty_line = "-" + empty_line = "-", + ... )) invisible(x) } diff --git a/R/describe_distribution.R b/R/describe_distribution.R index 2e61c1dc3..64f6e29c1 100644 --- a/R/describe_distribution.R +++ b/R/describe_distribution.R @@ -513,7 +513,7 @@ print.parameters_distribution <- function(x, digits = 2, ...) { ci_brackets = TRUE, ... ) - cat(insight::export_table(formatted_table, format = "text", digits = digits)) + cat(insight::export_table(formatted_table, format = "text", digits = digits, ...)) invisible(x) } diff --git a/man/data_codebook.Rd b/man/data_codebook.Rd index 319f4e5b6..d5a542be4 100644 --- a/man/data_codebook.Rd +++ b/man/data_codebook.Rd @@ -127,7 +127,8 @@ labels, values or value range, frequencies, amount of missing values). \note{ There are methods to \code{print()} the data frame in a nicer output, as well methods for printing in markdown or HTML format (\code{print_md()} and -\code{print_html()}). +\code{print_html()}). The \code{print()} method for text outputs passes arguments in +\code{...} to \code{\link[insight:export_table]{insight::export_table()}}. } \examples{ data(iris) diff --git a/man/data_tabulate.Rd b/man/data_tabulate.Rd index 032c0b989..b28a26ede 100644 --- a/man/data_tabulate.Rd +++ b/man/data_tabulate.Rd @@ -167,7 +167,8 @@ frequency tables as data frame. See 'Examples'. \note{ There are \code{print_html()} and \code{print_md()} methods available for printing frequency or crosstables in HTML and markdown format, e.g. -\code{print_html(data_tabulate(x))}. +\code{print_html(data_tabulate(x))}. The \code{print()} method for text outputs passes +arguments in \code{...} to \code{\link[insight:export_table]{insight::export_table()}}. } \section{Crosstables}{ diff --git a/tests/testthat/_snaps/data_codebook.md b/tests/testthat/_snaps/data_codebook.md index c390ba890..8f9b9e7b5 100644 --- a/tests/testthat/_snaps/data_codebook.md +++ b/tests/testthat/_snaps/data_codebook.md @@ -139,7 +139,7 @@ # data_codebook efc Code - data_codebook(efc) + print(data_codebook(efc), table_width = Inf) Output efc (100 rows and 5 variables, 5 shown) @@ -162,10 +162,94 @@ 5 | neg_c_7 | Negative impact with 7 items | numeric | 3 (3.0%) | [7, 28] | | 97 --------------------------------------------------------------------------------------------------------------------------------------------- +--- + + Code + print(data_codebook(efc), table_width = "auto", remove_duplicates = FALSE) + Output + efc (100 rows and 5 variables, 5 shown) + + ID | Name | Label | Type + ---+----------+------------------------------------------+------------ + 1 | c12hour | average number of hours of care per week | numeric + ---+----------+------------------------------------------+------------ + 2 | e16sex | elder's gender | numeric + | | | + ---+----------+------------------------------------------+------------ + 3 | e42dep | elder's dependency | categorical + | | | + | | | + | | | + ---+----------+------------------------------------------+------------ + 4 | c172code | carer's level of education | numeric + | | | + | | | + ---+----------+------------------------------------------+------------ + 5 | neg_c_7 | Negative impact with 7 items | numeric + ---------------------------------------------------------------------- + + ID | Missings | Values | Value Labels | N + ---+------------+----------+---------------------------------+----------- + 1 | 2 (2.0%) | [5, 168] | | 98 + ---+------------+----------+---------------------------------+----------- + 2 | 0 (0.0%) | 1 | male | 46 (46.0%) + | | 2 | female | 54 (54.0%) + ---+------------+----------+---------------------------------+----------- + 3 | 3 (3.0%) | 1 | independent | 2 ( 2.1%) + | | 2 | slightly dependent | 4 ( 4.1%) + | | 3 | moderately dependent | 28 (28.9%) + | | 4 | severely dependent | 63 (64.9%) + ---+------------+----------+---------------------------------+----------- + 4 | 10 (10.0%) | 1 | low level of education | 8 ( 8.9%) + | | 2 | intermediate level of education | 66 (73.3%) + | | 3 | high level of education | 16 (17.8%) + ---+------------+----------+---------------------------------+----------- + 5 | 3 (3.0%) | [7, 28] | | 97 + ------------------------------------------------------------------------- + +--- + + Code + print(data_codebook(efc), table_width = "auto", remove_duplicates = TRUE) + Output + efc (100 rows and 5 variables, 5 shown) + + ID | Name | Label | Type + ---+----------+------------------------------------------+------------ + 1 | c12hour | average number of hours of care per week | numeric + ---+----------+------------------------------------------+------------ + 2 | e16sex | elder's gender | numeric + ---+----------+------------------------------------------+------------ + 3 | e42dep | elder's dependency | categorical + ---+----------+------------------------------------------+------------ + 4 | c172code | carer's level of education | numeric + ---+----------+------------------------------------------+------------ + 5 | neg_c_7 | Negative impact with 7 items | numeric + ---------------------------------------------------------------------- + + ID | Missings | Values | Value Labels | N + ---+------------+----------+---------------------------------+----------- + 1 | 2 (2.0%) | [5, 168] | | 98 + ---+------------+----------+---------------------------------+----------- + 2 | 0 (0.0%) | 1 | male | 46 (46.0%) + | | 2 | female | 54 (54.0%) + ---+------------+----------+---------------------------------+----------- + 3 | 3 (3.0%) | 1 | independent | 2 ( 2.1%) + | | 2 | slightly dependent | 4 ( 4.1%) + | | 3 | moderately dependent | 28 (28.9%) + | | 4 | severely dependent | 63 (64.9%) + ---+------------+----------+---------------------------------+----------- + 4 | 10 (10.0%) | 1 | low level of education | 8 ( 8.9%) + | | 2 | intermediate level of education | 66 (73.3%) + | | 3 | high level of education | 16 (17.8%) + ---+------------+----------+---------------------------------+----------- + 5 | 3 (3.0%) | [7, 28] | | 97 + ------------------------------------------------------------------------- + # data_codebook efc, variable_label_width Code - data_codebook(efc, variable_label_width = 30) + print(out, table_width = Inf) Output efc (100 rows and 5 variables, 5 shown) @@ -189,10 +273,97 @@ 5 | neg_c_7 | Negative impact with 7 items | numeric | 3 (3.0%) | [7, 28] | | 97 --------------------------------------------------------------------------------------------------------------------------------- +--- + + Code + print(out, table_width = "auto", remove_duplicates = FALSE) + Output + efc (100 rows and 5 variables, 5 shown) + + ID | Name | Label | Type | Missings + ---+----------+------------------------------+-------------+----------- + 1 | c12hour | average number of hours of | numeric | 2 (2.0%) + | | care per week | | + ---+----------+------------------------------+-------------+----------- + 2 | e16sex | elder's gender | numeric | 0 (0.0%) + | | | | + ---+----------+------------------------------+-------------+----------- + 3 | e42dep | elder's dependency | categorical | 3 (3.0%) + | | | | + | | | | + | | | | + ---+----------+------------------------------+-------------+----------- + 4 | c172code | carer's level of education | numeric | 10 (10.0%) + | | | | + | | | | + ---+----------+------------------------------+-------------+----------- + 5 | neg_c_7 | Negative impact with 7 items | numeric | 3 (3.0%) + ----------------------------------------------------------------------- + + ID | Values | Value Labels | N + ---+----------+---------------------------------+----------- + 1 | [5, 168] | | 98 + | | | + ---+----------+---------------------------------+----------- + 2 | 1 | male | 46 (46.0%) + | 2 | female | 54 (54.0%) + ---+----------+---------------------------------+----------- + 3 | 1 | independent | 2 ( 2.1%) + | 2 | slightly dependent | 4 ( 4.1%) + | 3 | moderately dependent | 28 (28.9%) + | 4 | severely dependent | 63 (64.9%) + ---+----------+---------------------------------+----------- + 4 | 1 | low level of education | 8 ( 8.9%) + | 2 | intermediate level of education | 66 (73.3%) + | 3 | high level of education | 16 (17.8%) + ---+----------+---------------------------------+----------- + 5 | [7, 28] | | 97 + ------------------------------------------------------------ + +--- + + Code + print(out, table_width = "auto", remove_duplicates = TRUE) + Output + efc (100 rows and 5 variables, 5 shown) + + ID | Name | Label | Type | Missings + ---+----------+------------------------------+-------------+----------- + 1 | c12hour | average number of hours of | numeric | 2 (2.0%) + | | care per week | | + ---+----------+------------------------------+-------------+----------- + 2 | e16sex | elder's gender | numeric | 0 (0.0%) + ---+----------+------------------------------+-------------+----------- + 3 | e42dep | elder's dependency | categorical | 3 (3.0%) + ---+----------+------------------------------+-------------+----------- + 4 | c172code | carer's level of education | numeric | 10 (10.0%) + ---+----------+------------------------------+-------------+----------- + 5 | neg_c_7 | Negative impact with 7 items | numeric | 3 (3.0%) + ----------------------------------------------------------------------- + + ID | Values | Value Labels | N + ---+----------+---------------------------------+----------- + 1 | [5, 168] | | 98 + ---+----------+---------------------------------+----------- + 2 | 1 | male | 46 (46.0%) + | 2 | female | 54 (54.0%) + ---+----------+---------------------------------+----------- + 3 | 1 | independent | 2 ( 2.1%) + | 2 | slightly dependent | 4 ( 4.1%) + | 3 | moderately dependent | 28 (28.9%) + | 4 | severely dependent | 63 (64.9%) + ---+----------+---------------------------------+----------- + 4 | 1 | low level of education | 8 ( 8.9%) + | 2 | intermediate level of education | 66 (73.3%) + | 3 | high level of education | 16 (17.8%) + ---+----------+---------------------------------+----------- + 5 | [7, 28] | | 97 + ------------------------------------------------------------ + # data_codebook efc, value_label_width Code - data_codebook(efc, variable_label_width = 30, value_label_width = 15) + print(out, table_width = Inf) Output efc (100 rows and 5 variables, 5 shown) @@ -216,6 +387,93 @@ 5 | neg_c_7 | Negative impact with 7 items | numeric | 3 (3.0%) | [7, 28] | | 97 ------------------------------------------------------------------------------------------------------------------ +--- + + Code + print(out, table_width = "auto", remove_duplicates = FALSE) + Output + efc (100 rows and 5 variables, 5 shown) + + ID | Name | Label | Type | Missings + ---+----------+------------------------------+-------------+----------- + 1 | c12hour | average number of hours of | numeric | 2 (2.0%) + | | care per week | | + ---+----------+------------------------------+-------------+----------- + 2 | e16sex | elder's gender | numeric | 0 (0.0%) + | | | | + ---+----------+------------------------------+-------------+----------- + 3 | e42dep | elder's dependency | categorical | 3 (3.0%) + | | | | + | | | | + | | | | + ---+----------+------------------------------+-------------+----------- + 4 | c172code | carer's level of education | numeric | 10 (10.0%) + | | | | + | | | | + ---+----------+------------------------------+-------------+----------- + 5 | neg_c_7 | Negative impact with 7 items | numeric | 3 (3.0%) + ----------------------------------------------------------------------- + + ID | Values | Value Labels | N + ---+----------+------------------+----------- + 1 | [5, 168] | | 98 + | | | + ---+----------+------------------+----------- + 2 | 1 | male | 46 (46.0%) + | 2 | female | 54 (54.0%) + ---+----------+------------------+----------- + 3 | 1 | independent | 2 ( 2.1%) + | 2 | slightly... | 4 ( 4.1%) + | 3 | moderately... | 28 (28.9%) + | 4 | severely... | 63 (64.9%) + ---+----------+------------------+----------- + 4 | 1 | low level of... | 8 ( 8.9%) + | 2 | intermediate... | 66 (73.3%) + | 3 | high level of... | 16 (17.8%) + ---+----------+------------------+----------- + 5 | [7, 28] | | 97 + --------------------------------------------- + +--- + + Code + print(out, table_width = "auto", remove_duplicates = TRUE) + Output + efc (100 rows and 5 variables, 5 shown) + + ID | Name | Label | Type | Missings + ---+----------+------------------------------+-------------+----------- + 1 | c12hour | average number of hours of | numeric | 2 (2.0%) + | | care per week | | + ---+----------+------------------------------+-------------+----------- + 2 | e16sex | elder's gender | numeric | 0 (0.0%) + ---+----------+------------------------------+-------------+----------- + 3 | e42dep | elder's dependency | categorical | 3 (3.0%) + ---+----------+------------------------------+-------------+----------- + 4 | c172code | carer's level of education | numeric | 10 (10.0%) + ---+----------+------------------------------+-------------+----------- + 5 | neg_c_7 | Negative impact with 7 items | numeric | 3 (3.0%) + ----------------------------------------------------------------------- + + ID | Values | Value Labels | N + ---+----------+------------------+----------- + 1 | [5, 168] | | 98 + ---+----------+------------------+----------- + 2 | 1 | male | 46 (46.0%) + | 2 | female | 54 (54.0%) + ---+----------+------------------+----------- + 3 | 1 | independent | 2 ( 2.1%) + | 2 | slightly... | 4 ( 4.1%) + | 3 | moderately... | 28 (28.9%) + | 4 | severely... | 63 (64.9%) + ---+----------+------------------+----------- + 4 | 1 | low level of... | 8 ( 8.9%) + | 2 | intermediate... | 66 (73.3%) + | 3 | high level of... | 16 (17.8%) + ---+----------+------------------+----------- + 5 | [7, 28] | | 97 + --------------------------------------------- + # data_codebook truncated data Code diff --git a/tests/testthat/test-data_codebook.R b/tests/testthat/test-data_codebook.R index 26a67ccf6..06e9bd2f9 100644 --- a/tests/testthat/test-data_codebook.R +++ b/tests/testthat/test-data_codebook.R @@ -19,7 +19,7 @@ test_that("data_codebook NaN and Inf", { set.seed(123) d <- data.frame( - x = c(sample(1:15, 100, TRUE), Inf, Inf) + x = c(sample.int(15, 100, TRUE), Inf, Inf) ) expect_snapshot(data_codebook(d)) expect_snapshot(data_codebook(d, range_at = 100)) @@ -38,24 +38,32 @@ test_that("data_codebook iris, select, ID", { test_that("data_codebook efc", { - expect_snapshot(data_codebook(efc)) + expect_snapshot(print(data_codebook(efc), table_width = Inf)) + expect_snapshot(print(data_codebook(efc), table_width = "auto", remove_duplicates = FALSE)) + expect_snapshot(print(data_codebook(efc), table_width = "auto", remove_duplicates = TRUE)) }) test_that("data_codebook efc, variable_label_width", { - expect_snapshot(data_codebook(efc, variable_label_width = 30)) + out <- data_codebook(efc, variable_label_width = 30) + expect_snapshot(print(out, table_width = Inf)) + expect_snapshot(print(out, table_width = "auto", remove_duplicates = FALSE)) + expect_snapshot(print(out, table_width = "auto", remove_duplicates = TRUE)) }) test_that("data_codebook efc, value_label_width", { - expect_snapshot(data_codebook(efc, variable_label_width = 30, value_label_width = 15)) + out <- data_codebook(efc, variable_label_width = 30, value_label_width = 15) + expect_snapshot(print(out, table_width = Inf)) + expect_snapshot(print(out, table_width = "auto", remove_duplicates = FALSE)) + expect_snapshot(print(out, table_width = "auto", remove_duplicates = TRUE)) }) test_that("data_codebook truncated data", { set.seed(123) d <- data.frame( - a = sample(1:15, 100, TRUE), + a = sample.int(15, 100, TRUE), b = sample(letters[1:18], 100, TRUE), stringsAsFactors = FALSE ) @@ -66,7 +74,7 @@ test_that("data_codebook truncated data", { test_that("data_codebook mixed numeric lengths", { set.seed(123) d <- data.frame( - a = sample(1:4, 100, TRUE), + a = sample.int(4, 100, TRUE), b = sample(5:15, 100, TRUE), stringsAsFactors = FALSE ) @@ -76,7 +84,7 @@ test_that("data_codebook mixed numeric lengths", { test_that("data_codebook mixed range_at", { set.seed(123) d <- data.frame( - a = sample(1:4, 100, TRUE), + a = sample.int(4, 100, TRUE), b = sample(5:15, 100, TRUE), stringsAsFactors = FALSE ) @@ -87,7 +95,7 @@ test_that("data_codebook mixed range_at", { test_that("data_codebook logicals", { set.seed(123) d <- data.frame( - a = sample(1:15, 100, TRUE), + a = sample.int(15, 100, TRUE), b = sample(letters[1:3], 100, TRUE), c = sample(c(TRUE, FALSE), 100, TRUE), stringsAsFactors = FALSE @@ -99,14 +107,14 @@ test_that("data_codebook logicals", { test_that("data_codebook labelled data exceptions", { set.seed(123) - f1 <- sample(1:5, 100, TRUE) + f1 <- sample.int(5, 100, TRUE) f1[f1 == 4] <- NA attr(f1, "labels") <- setNames(1:5, c("One", "Two", "Three", "Four", "Five")) - f2 <- sample(1:5, 100, TRUE) + f2 <- sample.int(5, 100, TRUE) attr(f2, "labels") <- setNames(c(1:3, 5), c("One", "Two", "Three", "Five")) - f3 <- sample(1:5, 100, TRUE) + f3 <- sample.int(5, 100, TRUE) attr(f3, "labels") <- setNames(1:5, c("One", "Two", "Three", "Four", "Five")) d <- data.frame(f1, f2, f3) @@ -143,7 +151,7 @@ test_that("data_codebook works with numbers < 1", { test_that("data_codebook, big marks", { set.seed(123) f1 <- factor(sample(c("c", "b", "a"), 1e6, TRUE)) - f2 <- factor(sample(1:3, 1e6, TRUE)) + f2 <- factor(sample.int(3, 1e6, TRUE)) d <- data.frame(f1, f2) expect_snapshot(data_codebook(d)) })