Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if "weights" exists for vector-method #485

Merged
merged 10 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")),
Expand Down
2 changes: 1 addition & 1 deletion R/data_tabulate.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
35 changes: 33 additions & 2 deletions R/data_xtabulate.R
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,39 @@ print_html.dw_data_xtabulates <- function(x, big_mark = NULL, ...) {
}


.validate_table_weights <- function(weights, x) {
if (!is.null(weights)) {
.validate_table_weights <- function(weights, x, weights_expression = NULL) {
# 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)) {
# possibly misspelled weights-variables for default-method ----------------
# -------------------------------------------------------------------------

# 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 {
# possibly misspecified weights-variables for data.frame-method -----------
# -------------------------------------------------------------------------

if (is.character(weights)) {
# If "weights" is a character string, must be of length 1
if (length(weights) > 1) {
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-data_tabulate.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})


Expand Down
Loading