Skip to content

Commit c7b6a54

Browse files
Merge branch 'main' into rc_datawizard_1.2.0
2 parents c77e9ff + 10f0e86 commit c7b6a54

File tree

3 files changed

+80
-24
lines changed

3 files changed

+80
-24
lines changed

R/data_tabulate.R

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,12 @@ as.table.datawizard_table <- function(x, remove_na = TRUE, simplify = FALSE, ver
504504
if (!is.data.frame(x)) {
505505
x <- x[[1]]
506506
}
507+
# check if any table has NA values - the column "Value" contains the value
508+
# "NA", and the column "N" contains the frequency of this value.
507509
if (remove_na) {
508-
if (verbose) {
510+
# .check_table_na() works on lists of data frames, so we wrap the data frame
511+
# into a list here
512+
if (verbose && .check_table_na(list(x))) {
509513
insight::format_alert("Removing NA values from frequency table.")
510514
}
511515
# remove NA values from the table
@@ -524,7 +528,7 @@ as.table.datawizard_table <- function(x, remove_na = TRUE, simplify = FALSE, ver
524528
#' @export
525529
as.table.datawizard_tables <- function(x, remove_na = TRUE, simplify = FALSE, verbose = TRUE, ...) {
526530
# only show message once we set `verbose = FALSE` in the lapply()
527-
if (remove_na && verbose) {
531+
if (remove_na && verbose && .check_table_na(x)) {
528532
insight::format_alert("Removing NA values from frequency table.")
529533
}
530534

@@ -564,7 +568,7 @@ as.table.datawizard_crosstab <- function(x, remove_na = TRUE, simplify = FALSE,
564568
rownames(x) <- row_names
565569

566570
if (remove_na) {
567-
if (verbose) {
571+
if (verbose && .check_xtable_na(list(x))) {
568572
insight::format_alert("Removing NA values from frequency table.")
569573
}
570574
if (!is.null(x[["NA"]])) {
@@ -587,7 +591,7 @@ as.table.datawizard_crosstab <- function(x, remove_na = TRUE, simplify = FALSE,
587591
#' @export
588592
as.table.datawizard_crosstabs <- function(x, remove_na = TRUE, simplify = FALSE, verbose = TRUE, ...) {
589593
# only show message once we set `verbose = FALSE` in the lapply()
590-
if (remove_na && verbose) {
594+
if (remove_na && verbose && .check_xtable_na(x)) {
591595
insight::format_alert("Removing NA values from frequency table.")
592596
}
593597

@@ -613,6 +617,7 @@ as.table.datawizard_crosstabs <- function(x, remove_na = TRUE, simplify = FALSE,
613617
out
614618
}
615619

620+
616621
.is_grouped_df_xtab <- function(x) {
617622
if (!is.data.frame(x)) {
618623
x <- x[[1]]
@@ -621,6 +626,37 @@ as.table.datawizard_crosstabs <- function(x, remove_na = TRUE, simplify = FALSE,
621626
}
622627

623628

629+
.check_table_na <- function(x) {
630+
# check if any table has NA values - the column "Value" contains the value
631+
# "NA", and the column "N" contains the frequency of this value.
632+
any(vapply(x, function(i) any(i$N[is.na(i$Value)] > 0), logical(1)))
633+
}
634+
635+
636+
.check_xtable_na <- function(x) {
637+
any(vapply(x, function(i) {
638+
# need to extract rownames, to check if we have a "NA" row
639+
row_names <- as.character(i[[1]])
640+
row_names[is.na(row_names)] <- "NA"
641+
has_na <- FALSE
642+
# check for NA columns and rows
643+
if (!is.null(i[["NA"]])) {
644+
has_na <- any(i[["NA"]] > 0)
645+
}
646+
if ("NA" %in% row_names) {
647+
# for grouped data frames, we need to remove the "Group" column, else
648+
# the indexing -1 below won't work
649+
if (.is_grouped_df_xtab(i)) {
650+
i$Group <- NULL
651+
}
652+
# we need "as.data.frame()" for grouped df, else `as.vector()` fails
653+
has_na <- has_na | any(as.vector(as.data.frame(i[row_names == "NA", -1])) > 0)
654+
}
655+
has_na
656+
}, logical(1)))
657+
}
658+
659+
624660
# format --------------------
625661

626662
#' @export

tests/testthat/_snaps/data_tabulate.md

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -699,8 +699,6 @@
699699

700700
Code
701701
as.table(x)
702-
Message
703-
Removing NA values from frequency table.
704702
Output
705703
[[1]]
706704
4 6 8
@@ -711,8 +709,6 @@
711709

712710
Code
713711
as.table(x)
714-
Message
715-
Removing NA values from frequency table.
716712
Output
717713
[[1]]
718714
4 6 8
@@ -733,8 +729,6 @@
733729

734730
Code
735731
as.table(x)
736-
Message
737-
Removing NA values from frequency table.
738732
Output
739733
[[1]]
740734
4 6 8
@@ -749,8 +743,6 @@
749743

750744
Code
751745
as.table(x)
752-
Message
753-
Removing NA values from frequency table.
754746
Output
755747
[[1]]
756748
3 4 5
@@ -763,8 +755,6 @@
763755

764756
Code
765757
as.table(x, simplify = TRUE)
766-
Message
767-
Removing NA values from frequency table.
768758
Output
769759
3 4 5
770760
4 1 8 2
@@ -775,8 +765,6 @@
775765

776766
Code
777767
as.table(x)
778-
Message
779-
Removing NA values from frequency table.
780768
Output
781769
[[1]]
782770
3 4 5
@@ -789,8 +777,6 @@
789777

790778
Code
791779
as.table(x, simplify = TRUE)
792-
Message
793-
Removing NA values from frequency table.
794780
Output
795781
3 4 5
796782
4 1 8 2
@@ -801,8 +787,6 @@
801787

802788
Code
803789
as.table(x)
804-
Message
805-
Removing NA values from frequency table.
806790
Output
807791
[[1]]
808792
3 4 5
@@ -820,8 +804,6 @@
820804

821805
Code
822806
as.table(x)
823-
Message
824-
Removing NA values from frequency table.
825807
Output
826808
$`am (0)`
827809
3 4

tests/testthat/test-data_tabulate.R

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,45 @@ test_that("data_tabulate, table methods", {
573573
expect_named(as.table(x), c("am (0)", "am (1)"))
574574
expect_snapshot(as.table(x))
575575

576-
# messages
577-
expect_message(as.table(data_tabulate(mtcars, "cyl")), regex = "Removing NA values")
576+
# messages - no missings to remove
577+
expect_silent(as.table(data_tabulate(mtcars, "cyl")))
578578
expect_silent(as.table(data_tabulate(mtcars, "cyl"), verbose = FALSE))
579579
})
580+
581+
582+
test_that("data_tabulate, table methods, only warn if necessary", {
583+
# missings
584+
data(efc)
585+
586+
# single variable
587+
expect_message(as.table(data_tabulate(efc$c172code)))
588+
expect_silent(as.table(data_tabulate(efc$c172code, remove_na = TRUE)))
589+
expect_silent(as.table(data_tabulate(efc$c172code), remove_na = FALSE))
590+
expect_silent(as.table(data_tabulate(efc$c172code), verbose = FALSE))
591+
592+
# cross table
593+
expect_message(
594+
as.table(data_tabulate(efc, "c172code", by = "e42dep")),
595+
regex = "Removing NA values"
596+
)
597+
expect_silent(as.table(data_tabulate(efc, "c172code", by = "e42dep", remove_na = TRUE)))
598+
expect_silent(as.table(data_tabulate(efc, "c172code", by = "e42dep"), remove_na = FALSE))
599+
expect_silent(as.table(data_tabulate(efc, "c172code", by = "e42dep"), verbose = FALSE))
600+
601+
# no missings
602+
data(mtcars)
603+
604+
# single variable
605+
expect_silent(as.table(data_tabulate(mtcars$gear)))
606+
expect_silent(as.table(data_tabulate(mtcars$gear, remove_na = TRUE)))
607+
expect_silent(as.table(data_tabulate(mtcars$gear), verbose = FALSE))
608+
609+
# cross table
610+
expect_silent(as.table(data_tabulate(mtcars, "gear", by = "cyl")))
611+
expect_silent(as.table(data_tabulate(mtcars, "gear", by = "cyl", remove_na = TRUE)))
612+
expect_silent(as.table(data_tabulate(mtcars, "gear", by = "cyl"), verbose = FALSE))
613+
614+
# group DF throws no warning
615+
d <- data_group(mtcars, "am")
616+
expect_silent(as.table(data_tabulate(d, "cyl", by = "gear")))
617+
})

0 commit comments

Comments
 (0)