diff --git a/R/count_bindings.R b/R/count_bindings.R index 5601cd90..291779e3 100644 --- a/R/count_bindings.R +++ b/R/count_bindings.R @@ -576,6 +576,10 @@ set_outer_sort_position <- function(e, outer_sort_position) { set_denom_where <- function(e, denom_where) { denom_where <- enquo(denom_where) + if (!(inherits(e, 'tplyr_layer') | inherits(e, 'tplyr_table'))) { + stop('Object type should be either "tplyr_layer" or "tplyr_table"', call.=FALSE) + } + assert_that(is_logical_or_call(denom_where), msg = "The `where` parameter must contain subsetting logic (enter without quotes)") diff --git a/tests/testthat/test-count.R b/tests/testthat/test-count.R index c61f0d29..3a248a2e 100644 --- a/tests/testthat/test-count.R +++ b/tests/testthat/test-count.R @@ -440,6 +440,17 @@ test_that("set_denom_where works as expected", { expect_snapshot_output(dput(t13)) }) +test_that("set_denom_where errors for incompatible object type", { + t1 <- tplyr_table(mtcars, gear) + + # Modify the object type to make it incompatible + class(t1) <- "environment" + + # Function errors + t1 <- set_denom_where(t1, mpg != 21) %>% + expect_error("Object type should be") +}) + test_that("missing counts can be set without a format and it inherits the layer format", { t1 <- tplyr_table(mtcars, gear) %>% add_layer(