From 1597f84a3da5fa674932d0525bdbcb3cf1290716 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 7 Mar 2024 13:15:47 +0100 Subject: [PATCH] refactor --- R/data_xtabulate.R | 52 +++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/R/data_xtabulate.R b/R/data_xtabulate.R index 2a116d543..f392cae6b 100644 --- a/R/data_xtabulate.R +++ b/R/data_xtabulate.R @@ -313,7 +313,32 @@ print_html.dw_data_xtabulates <- function(x, big_mark = NULL, ...) { .validate_table_weights <- function(weights, x, weights_expression = NULL) { - if (!is.null(weights)) { # nolint + # exception: for vectors, if weighting variable not found, "weights" is NULL. + # to check this, we further need to check whether a weights expression was + # provided, e.g. "weights = iris$not_found" - all this is only relevant when + # weights is NULL + if (is.null(weights)) { + # do we have any value for weights_expression? + if (!is.null(weights_expression) && + # due to deparse() and substitute, NULL becomes "NULL" - we need to check for this + !identical(weights_expression, "NULL") && + # we should only run into this problem, when a variable from a data frame + # is used in the data_tabulate() method for vectors - thus, we need to check + # whether the weights_expression contains a "$" - `iris$not_found` is "NULL" + # we need this check, because the default-method of data_tabulate() is called + # from the data.frame method, where `weights = weights`, and then, + # deparse(substitute(weights)) is "weights" (not "NULL" or "iris$not_found"), + # leading to an error when actually all is OK (if "weights" is NULL) + # Example: + #> efc$weights <- abs(rnorm(n = nrow(efc), mean = 1, sd = 0.5)) + # Here, efc$wweight is NULL + #> data_tabulate(efc$c172code, weights = efc$wweight) + # Here, wweight errors anyway, because object "wweight" is not found + #> data_tabulate(efc$c172code, weights = wweight) + grepl("$", weights_expression, fixed = TRUE)) { + insight::format_error("The variable specified in `weights` was not found. Possibly misspelled?") + } + } else { if (is.character(weights)) { # If "weights" is a character string, must be of length 1 if (length(weights) > 1) { @@ -341,31 +366,6 @@ print_html.dw_data_xtabulates <- function(x, big_mark = NULL, ...) { if (!is.data.frame(x) && length(weights) != length(x)) { insight::format_error("Length of `weights` must be equal to length of `x`.") # nolint } - - # 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" - all this is only relevant when - # weights is NULL - - # do we have any value for weights_expression? - } else if (!is.null(weights_expression) && - # due to deparse() and substitute, NULL becomes "NULL" - we need to check for this - !identical(weights_expression, "NULL") && - # we should only run into this problem, when a variable from a data frame - # is used in the data_tabulate() method for vectors - thus, we need to check - # whether the weights_expression contains a "$" - `iris$not_found` is "NULL" - # we need this check, because the default-method of data_tabulate() is called - # from the data.frame method, where `weights = weights`, and then, - # deparse(substitute(weights)) is "weights" (not "NULL" or "iris$not_found"), - # leading to an error when it's actually not (if "weights" is NULL) - # Example: - #> efc$weights <- abs(rnorm(n = nrow(efc), mean = 1, sd = 0.5)) - # Here, efc$wweight is NULL - #> data_tabulate(efc$c172code, weights = efc$wweight) - # Here, wweight errors anyway, because object "wweight" is not found - #> data_tabulate(efc$c172code, weights = wweight) - grepl("$", weights_expression, fixed = TRUE)) { - insight::format_error("The variable specified in `weights` was not found. Possibly misspelled?") } weights