Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Mar 7, 2024
1 parent 9b11b0b commit 1597f84
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions R/data_xtabulate.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1597f84

Please sign in to comment.