Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion R/conttables.b.R
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ contTablesClass <- R6::R6Class(
jmvcore::reject(.('Counts may not be negative'))
if (any(is.infinite(data$.COUNTS)))
jmvcore::reject(.('Counts may not be infinite'))
if (any(is.na(data$.COUNTS)))
jmvcore::reject(.('Counts may not contain missing values'))
}

freqs <- self$results$freqs
Expand Down Expand Up @@ -363,7 +365,7 @@ contTablesClass <- R6::R6Class(
} else
MCpsimul <- FALSE

if (all(dim(mat) == 2) && all(rowSums(mat) > 0) && all(colSums(mat) > 0)) {
if (isTRUE(all(dim(mat) == 2) && all(rowSums(mat) > 0) && all(colSums(mat) > 0))) {
dp <- private$.diffProp(mat, Ha)
lor <- vcd::loddsratio(mat)
rr <- private$.relativeRisk(mat)
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/testconttables.R
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,19 @@ testthat::test_that("bar plots work with spaces in variable name", {

testthat::expect_true(table$barplot$.render())
})

testthat::test_that("conttables rejects NA counts with a clear error", {
# GIVEN a 2x2 contingency table where one count value is NA
rows <- factor(c("A", "B", "A", "B"), c("A", "B"))
cols <- factor(c("1", "1", "2", "2"), c("1", "2"))
counts <- c(72, 48, 45, NA)
data <- data.frame(rows = rows, cols = cols, counts = counts)

# WHEN running contTables with a counts variable containing NA
# THEN the analysis is rejected with a clear error message
testthat::expect_error(
jmv::contTables(data=data, rows="rows", cols="cols", counts="counts"),
regexp='missing values',
ignore.case=TRUE
)
})
Loading