From 8194b515506c98acfc6ca31cc40c26e88d065944 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 8 Nov 2024 22:33:42 +0100 Subject: [PATCH] Fix issue with date-variables for compact-functions (#966) * Fix issue with date-variables for compact-functions * lintr --- DESCRIPTION | 2 +- NEWS.md | 2 ++ R/utils_compact.R | 6 +++--- tests/testthat/test-compact-character.R | 12 ++++++++---- tests/testthat/test-compact-list.R | 7 +++++++ 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 1818dc5a30..fd7d2ee464 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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", diff --git a/NEWS.md b/NEWS.md index 6382f74420..bc57ce8403 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/utils_compact.R b/R/utils_compact.R index 0de7a27c61..e6e562b03e 100644 --- a/R/utils_compact.R +++ b/R/utils_compact.R @@ -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)))] } } @@ -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))] } diff --git a/tests/testthat/test-compact-character.R b/tests/testthat/test-compact-character.R index b6bc1cabfb..2a797ea861 100644 --- a/tests/testthat/test-compact-character.R +++ b/tests/testthat/test-compact-character.R @@ -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")) }) diff --git a/tests/testthat/test-compact-list.R b/tests/testthat/test-compact-list.R index 1074b22d13..c93124b6fe 100644 --- a/tests/testthat/test-compact-list.R +++ b/tests/testthat/test-compact-list.R @@ -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", {