Skip to content

Commit

Permalink
simplify nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Mar 7, 2024
1 parent acb33ae commit 9b11b0b
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions R/data_xtabulate.R
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ print_html.dw_data_xtabulates <- function(x, big_mark = NULL, ...) {


.validate_table_weights <- function(weights, x, weights_expression = NULL) {
if (!is.null(weights)) {
if (!is.null(weights)) { # nolint
if (is.character(weights)) {
# If "weights" is a character string, must be of length 1
if (length(weights) > 1) {
Expand Down Expand Up @@ -341,14 +341,14 @@ 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"
# 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?
if (!is.null(weights_expression) &&
# 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
Expand All @@ -364,9 +364,7 @@ print_html.dw_data_xtabulates <- function(x, big_mark = 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) &&
# if all the above apply, weights must be NULL - only error in this case
is.null(weights)) {
grepl("$", weights_expression, fixed = TRUE)) {
insight::format_error("The variable specified in `weights` was not found. Possibly misspelled?")
}

Expand Down

0 comments on commit 9b11b0b

Please sign in to comment.