Skip to content

Commit

Permalink
Fix snapshot tests (#560)
Browse files Browse the repository at this point in the history
* Fix snapshot tests
Fixes #559

* update

* pass ... to export_table

* update tests

* pass ...

* update tests

* docs

* Trigger CI

* lintr
  • Loading branch information
strengejacke authored Nov 4, 2024
1 parent 5703d85 commit b723df2
Show file tree
Hide file tree
Showing 9 changed files with 299 additions and 25 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0003-1995-6531")),
Expand Down
6 changes: 4 additions & 2 deletions R/data_codebook.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -369,7 +370,8 @@ print.data_codebook <- function(x, ...) {
title = caption,
empty_line = "-",
cross = "+",
align = .get_codebook_align(x)
align = .get_codebook_align(x),
...
)
)
}
Expand Down
9 changes: 6 additions & 3 deletions R/data_tabulate.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -522,7 +523,8 @@ print.datawizard_table <- function(x, big_mark = NULL, ...) {
cat(insight::export_table(
format(x, big_mark = big_mark, ...),
cross = "+",
missing = "<NA>"
missing = "<NA>",
...
))
invisible(x)
}
Expand Down Expand Up @@ -621,7 +623,8 @@ print.datawizard_tables <- function(x, big_mark = NULL, ...) {
out,
missing = "<NA>",
cross = "+",
empty_line = "-"
empty_line = "-",
...
))
}
}
Expand Down
3 changes: 2 additions & 1 deletion R/data_xtabulate.R
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ print.datawizard_crosstab <- function(x, big_mark = NULL, ...) {
cross = "+",
missing = "<NA>",
caption = caption,
empty_line = "-"
empty_line = "-",
...
))
invisible(x)
}
Expand Down
2 changes: 1 addition & 1 deletion R/describe_distribution.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
3 changes: 2 additions & 1 deletion man/data_codebook.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/data_tabulate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

264 changes: 261 additions & 3 deletions tests/testthat/_snaps/data_codebook.md

Large diffs are not rendered by default.

32 changes: 20 additions & 12 deletions tests/testthat/test-data_codebook.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
)
Expand All @@ -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
)
Expand All @@ -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
)
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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))
})
Expand Down

0 comments on commit b723df2

Please sign in to comment.