Skip to content
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
13 changes: 7 additions & 6 deletions R/ascii_tables.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -53,7 +52,7 @@ invert <- function(x) {
x2[[i]][[j]] <- x[[j]][[i]]
}
}
return(x2)
x2
}


Expand Down Expand Up @@ -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
}


Expand All @@ -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")
Copy link
Owner Author

@gowerc gowerc Aug 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think from memory this code exists (as opposed to just using as.character() ) to force the timestamp to be shown. Regular dates already have the NA conversion covered within the default as.character() handler but for some reason that was missed here

x[is.na(x)] <- "<NA>"
x
}


Expand Down Expand Up @@ -270,5 +271,5 @@ get_table <- function(dsin, row_limit = 10) {
),
collapse = "\n"
)
return(msg)
msg
}
156 changes: 156 additions & 0 deletions tests/testthat/_snaps/miscellaneous.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <NA>
-----------------------------------------------------------



---

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 <NA>
dt1 2 2024-01-24 14:12:49 UTC <NA>
dt1 3 1821-02-01 01:01:01 UTC <NA>
-----------------------------------------------------------



# `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 <NA>
----------------------------------------------



---

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 <NA>
dt1 2 2024-01-24 <NA>
dt1 3 1821-02-01 <NA>
----------------------------------------------



84 changes: 84 additions & 0 deletions tests/testthat/test-miscellaneous.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
Loading