diff --git a/DESCRIPTION b/DESCRIPTION index eeee80702..34966d0e6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: datawizard Title: Easy Data Wrangling and Statistical Transformations -Version: 0.9.1.5 +Version: 0.9.1.6 Authors@R: c( person("Indrajeet", "Patil", , "patilindrajeet.science@gmail.com", role = "aut", comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")), diff --git a/R/data_tabulate.R b/R/data_tabulate.R index 04f205ec8..76c9733b1 100644 --- a/R/data_tabulate.R +++ b/R/data_tabulate.R @@ -141,7 +141,7 @@ data_tabulate.default <- function(x, } # validate "weights" - weights <- .validate_table_weights(weights, x) + weights <- .validate_table_weights(weights, x, weights_expression = insight::safe_deparse(substitute(weights))) # we go into another function for crosstables here... if (!is.null(by)) { diff --git a/R/data_xtabulate.R b/R/data_xtabulate.R index c8e622e2d..aae2e5e57 100644 --- a/R/data_xtabulate.R +++ b/R/data_xtabulate.R @@ -312,7 +312,7 @@ print_html.dw_data_xtabulates <- function(x, big_mark = NULL, ...) { } -.validate_table_weights <- function(weights, x) { +.validate_table_weights <- function(weights, x, weights_expression = NULL) { if (!is.null(weights)) { if (is.character(weights)) { # If "weights" is a character string, must be of length 1 @@ -343,5 +343,12 @@ print_html.dw_data_xtabulates <- function(x, big_mark = NULL, ...) { } } + # exception: for vectors, if weighting variable not found, "weights" is NULL + # to check this, we check whether a weights expression was provided and weights + # is NULL, e.g. "weights = iris$not_found" + if (!is.null(weights_expression) && !identical(weights_expression, "NULL") && is.null(weights)) { + insight::format_error("The variable specified in `weights` was not found in `x`. Possibly misspelled?") + } + weights } diff --git a/tests/testthat/test-data_tabulate.R b/tests/testthat/test-data_tabulate.R index 1ca47a967..7b78e06b9 100644 --- a/tests/testthat/test-data_tabulate.R +++ b/tests/testthat/test-data_tabulate.R @@ -363,6 +363,7 @@ test_that("data_tabulate, cross tables, errors weights", { expect_error(data_tabulate(efc, "c172code", weights = efc$weights[-1]), regex = "Length of `weights`") expect_error(data_tabulate(efc, "c172code", weights = "weigths"), regex = "not found") expect_error(data_tabulate(efc, "c172code", weights = c("e16sex", "e42dep")), regex = "length 1") + expect_error(data_tabulate(efc$c172code, weights = efc$wweight), regex = "not found") })