diff --git a/R/ascii_tables.R b/R/ascii_tables.R index 24270843..a9a76e78 100644 --- a/R/ascii_tables.R +++ b/R/ascii_tables.R @@ -32,9 +32,8 @@ recursive_reduce <- function(.l, .f) { if (length(.l) != 1) { .l[[2]] <- .f(.l[[1]], .l[[2]]) return(recursive_reduce(.l[-1], .f)) - } else { - return(.l[[1]]) } + .l[[1]] } #' invert @@ -53,7 +52,7 @@ invert <- function(x) { x2[[i]][[j]] <- x[[j]][[i]] } } - return(x2) + x2 } @@ -202,7 +201,7 @@ as_fmt_char.character <- function(x, add_quotes = TRUE, crop_at = 30, ...) { # clearly identified in the printed output x[needs_quotes] <- paste0('"', x[needs_quotes], '"') - return(x) + x } @@ -224,7 +223,9 @@ as_fmt_char.default <- function(x, ...) { #' @rdname as_fmt_char #' @export as_fmt_char.POSIXt <- function(x, ...) { - format(x, "%Y-%m-%d %H:%M:%S %Z") + x <- format(x, "%Y-%m-%d %H:%M:%S %Z") + x[is.na(x)] <- "" + x } @@ -270,5 +271,5 @@ get_table <- function(dsin, row_limit = 10) { ), collapse = "\n" ) - return(msg) + msg } diff --git a/tests/testthat/_snaps/miscellaneous.md b/tests/testthat/_snaps/miscellaneous.md index d443a7c5..b1982556 100644 --- a/tests/testthat/_snaps/miscellaneous.md +++ b/tests/testthat/_snaps/miscellaneous.md @@ -305,3 +305,159 @@ +# `as_ascii_table() can handle missing datetimes (#132) + + Code + diffdf(d1, d2, suppress_warnings = TRUE) + Output + Differences found between the objects! + + Summary of BASE and COMPARE + ================================================================== + PROPERTY BASE COMP + ------------------------------------------------------------------ + Name d1 d2 + Class "tbl_df, tbl, data.frame" "tbl_df, tbl, data.frame" + Rows(#) 3 3 + Columns(#) 2 2 + ------------------------------------------------------------------ + + + Not all Values Compared Equal + ============================= + Variable No of Differences + ----------------------------- + id 1 + dt1 1 + ----------------------------- + + + ======================================== + VARIABLE ..ROWNUMBER.. BASE COMPARE + ---------------------------------------- + id 2 2 NA + ---------------------------------------- + + + =========================================================== + VARIABLE ..ROWNUMBER.. BASE COMPARE + ----------------------------------------------------------- + dt1 2 2024-01-24 14:12:49 UTC + ----------------------------------------------------------- + + + +--- + + Code + diffdf(d1, d3, suppress_warnings = TRUE) + Output + Differences found between the objects! + + Summary of BASE and COMPARE + ================================================================== + PROPERTY BASE COMP + ------------------------------------------------------------------ + Name d1 d3 + Class "tbl_df, tbl, data.frame" "tbl_df, tbl, data.frame" + Rows(#) 3 3 + Columns(#) 2 2 + ------------------------------------------------------------------ + + + Not all Values Compared Equal + ============================= + Variable No of Differences + ----------------------------- + dt1 3 + ----------------------------- + + + =========================================================== + VARIABLE ..ROWNUMBER.. BASE COMPARE + ----------------------------------------------------------- + dt1 1 2024-01-10 01:02:03 UTC + dt1 2 2024-01-24 14:12:49 UTC + dt1 3 1821-02-01 01:01:01 UTC + ----------------------------------------------------------- + + + +# `as_ascii_table() can handle missing dates (#132) + + Code + diffdf(d1, d2, suppress_warnings = TRUE) + Output + Differences found between the objects! + + Summary of BASE and COMPARE + ================================================================== + PROPERTY BASE COMP + ------------------------------------------------------------------ + Name d1 d2 + Class "tbl_df, tbl, data.frame" "tbl_df, tbl, data.frame" + Rows(#) 3 3 + Columns(#) 2 2 + ------------------------------------------------------------------ + + + Not all Values Compared Equal + ============================= + Variable No of Differences + ----------------------------- + id 1 + dt1 1 + ----------------------------- + + + ======================================== + VARIABLE ..ROWNUMBER.. BASE COMPARE + ---------------------------------------- + id 2 2 NA + ---------------------------------------- + + + ============================================== + VARIABLE ..ROWNUMBER.. BASE COMPARE + ---------------------------------------------- + dt1 2 2024-01-24 + ---------------------------------------------- + + + +--- + + Code + diffdf(d1, d3, suppress_warnings = TRUE) + Output + Differences found between the objects! + + Summary of BASE and COMPARE + ================================================================== + PROPERTY BASE COMP + ------------------------------------------------------------------ + Name d1 d3 + Class "tbl_df, tbl, data.frame" "tbl_df, tbl, data.frame" + Rows(#) 3 3 + Columns(#) 2 2 + ------------------------------------------------------------------ + + + Not all Values Compared Equal + ============================= + Variable No of Differences + ----------------------------- + dt1 3 + ----------------------------- + + + ============================================== + VARIABLE ..ROWNUMBER.. BASE COMPARE + ---------------------------------------------- + dt1 1 2024-01-10 + dt1 2 2024-01-24 + dt1 3 1821-02-01 + ---------------------------------------------- + + + diff --git a/tests/testthat/test-miscellaneous.R b/tests/testthat/test-miscellaneous.R index 9ae054eb..ae6df5bc 100644 --- a/tests/testthat/test-miscellaneous.R +++ b/tests/testthat/test-miscellaneous.R @@ -252,3 +252,87 @@ test_that("datetimes compare as expected", { print(res) ) }) + + + + +test_that("`as_ascii_table() can handle missing datetimes (#132)", { + d1 <- tibble( + id = c(1, 2, 3), + dt1 = lubridate::ymd_hms( + "2024-01-10 01-02-03", + "2024-01-24 14-12-49", + "1821-02-01 01-01-01" + ) + ) + + d2 <- tibble( + id = c(1, NA, 3), + dt1 = lubridate::ymd_hms( + "2024-01-10 01-02-03", + NA, + "1821-02-01 01-01-01" + ) + ) + expect_snapshot(diffdf(d1, d2, suppress_warnings = TRUE)) + + + + d3 <- tibble( + id = c(1, 2, 3), + dt1 = lubridate::ymd_hms(NA, NA, NA) + ) + expect_snapshot(diffdf(d1, d3, suppress_warnings = TRUE)) + + + set.seed(2211) + offset <- 2000000000 + n <- 50 + d5 <- tibble( + id = seq_len(n), + dt1 = lubridate::ymd_hms("2000-01-01 01-01-01") + round(runif(n, -offset, offset)) + ) + d6 <- d5 + d6[seq(1, n, by = 2), "dt1"] <- NA + expect_snapshot(diffdf(d5, d6, suppress_warnings = TRUE)) +}) + + +test_that("`as_ascii_table() can handle missing dates (#132)", { + d1 <- tibble( + id = c(1, 2, 3), + dt1 = lubridate::ymd( + "2024-01-10", + "2024-01-24", + "1821-02-01" + ) + ) + + d2 <- tibble( + id = c(1, NA, 3), + dt1 = lubridate::ymd( + "2024-01-10", + NA, + "1821-02-01" + ) + ) + expect_snapshot(diffdf(d1, d2, suppress_warnings = TRUE)) + + d3 <- tibble( + id = c(1, 2, 3), + dt1 = lubridate::ymd(NA, NA, NA) + ) + expect_snapshot(diffdf(d1, d3, suppress_warnings = TRUE)) + + + set.seed(2211) + offset <- 200000 + n <- 50 + d5 <- tibble( + id = seq_len(n), + dt1 = lubridate::ymd("2000-01-01") + round(runif(n, -offset, offset)) + ) + d6 <- d5 + d6[seq(1, n, by = 2), "dt1"] <- NA + expect_snapshot(diffdf(d5, d6, suppress_warnings = TRUE)) +})