Skip to content

Commit

Permalink
Fix issue with date-variables for compact-functions (#966)
Browse files Browse the repository at this point in the history
* Fix issue with date-variables for compact-functions

* lintr
  • Loading branch information
strengejacke authored Nov 8, 2024
1 parent e61c9f6 commit 8194b51
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: insight
Title: Easy Access to Model Information for Various Model Objects
Version: 0.99.0.12
Version: 0.99.0.13
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
* Fixed issues in `get_variance()` for models of class *brmsfit* when the
sigma-parameter was directly modeled.

* Fixed issue in `compact_character()` and `compact_list()` for date-variables.

# insight 0.20.5

## General
Expand Down
6 changes: 3 additions & 3 deletions R/utils_compact.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#' @export
compact_list <- function(x, remove_na = FALSE) {
if (remove_na) {
x[!sapply(x, function(i) !is_model(i) && !inherits(i, c("Formula", "gFormula")) && (length(i) == 0L || is.null(i) || (length(i) == 1L && is.na(i)) || all(is.na(i)) || any(i == "NULL", na.rm = TRUE)))]
x[!sapply(x, function(i) !is_model(i) && !inherits(i, c("Formula", "gFormula")) && (length(i) == 0L || is.null(i) || (length(i) == 1L && is.na(i)) || all(is.na(i)) || any(as.character(i) == "NULL", na.rm = TRUE)))]
} else {
x[!sapply(x, function(i) !is_model(i) && !inherits(i, c("Formula", "gFormula")) && (length(i) == 0L || is.null(i) || any(i == "NULL", na.rm = TRUE)))]
x[!sapply(x, function(i) !is_model(i) && !inherits(i, c("Formula", "gFormula")) && (length(i) == 0L || is.null(i) || any(as.character(i) == "NULL", na.rm = TRUE)))]
}
}

Expand All @@ -30,5 +30,5 @@ compact_list <- function(x, remove_na = FALSE) {
#'
#' @export
compact_character <- function(x) {
x[!sapply(x, function(i) !nzchar(i, keepNA = TRUE) || all(is.na(i)) || any(i == "NULL", na.rm = TRUE))]
x[!sapply(x, function(i) !nzchar(i, keepNA = TRUE) || all(is.na(i)) || any(as.character(i) == "NULL", na.rm = TRUE))]
}
12 changes: 8 additions & 4 deletions tests/testthat/test-compact-character.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
test_that("compact character works as expected", {
expect_equal(compact_character(c("x", "y", NA)), c("x", "y"))
expect_equal(compact_character(c("x", "y", NA_real_)), c("x", "y"))
expect_equal(compact_character(c("x", "NULL", "", "y")), c("x", "y"))
expect_identical(compact_character(c("x", "y", NA)), c("x", "y"))
expect_identical(compact_character(c("x", "y", NA_real_)), c("x", "y"))
expect_identical(compact_character(c("x", "NULL", "", "y")), c("x", "y"))

# scalar
expect_equal(compact_character(""), character(0))
expect_identical(compact_character(""), character(0))

# date
x <- as.Date(c("1900-05-06", "", "1900-05-07", NA))
expect_identical(compact_character(x), structure(c(-25442, -25441), class = "Date"))
})
7 changes: 7 additions & 0 deletions tests/testthat/test-compact-list.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ test_that("compact_list works as expected", {
expect_identical(compact_list(""), "")
expect_null(compact_list(NULL))
expect_identical(compact_list(logical(0)), logical(0))
# date
x <- as.Date(c("1900-05-06", "", "1900-05-07", NA))
y <- c(NA, 1, 5)
expect_identical(
compact_list(list(x, NULL, y)),
list(structure(c(-25442, NA, -25441, NA), class = "Date"), c(NA, 1, 5))
)
})

test_that("compact_list, logical > 1", {
Expand Down

0 comments on commit 8194b51

Please sign in to comment.