Skip to content

Commit 389c785

Browse files
authored
Fix datetime printing issue (#134)
1 parent c534d8a commit 389c785

File tree

3 files changed

+247
-6
lines changed

3 files changed

+247
-6
lines changed

R/ascii_tables.R

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ recursive_reduce <- function(.l, .f) {
3232
if (length(.l) != 1) {
3333
.l[[2]] <- .f(.l[[1]], .l[[2]])
3434
return(recursive_reduce(.l[-1], .f))
35-
} else {
36-
return(.l[[1]])
3735
}
36+
.l[[1]]
3837
}
3938

4039
#' invert
@@ -53,7 +52,7 @@ invert <- function(x) {
5352
x2[[i]][[j]] <- x[[j]][[i]]
5453
}
5554
}
56-
return(x2)
55+
x2
5756
}
5857

5958

@@ -202,7 +201,7 @@ as_fmt_char.character <- function(x, add_quotes = TRUE, crop_at = 30, ...) {
202201
# clearly identified in the printed output
203202
x[needs_quotes] <- paste0('"', x[needs_quotes], '"')
204203

205-
return(x)
204+
x
206205
}
207206

208207

@@ -224,7 +223,9 @@ as_fmt_char.default <- function(x, ...) {
224223
#' @rdname as_fmt_char
225224
#' @export
226225
as_fmt_char.POSIXt <- function(x, ...) {
227-
format(x, "%Y-%m-%d %H:%M:%S %Z")
226+
x <- format(x, "%Y-%m-%d %H:%M:%S %Z")
227+
x[is.na(x)] <- "<NA>"
228+
x
228229
}
229230

230231

@@ -270,5 +271,5 @@ get_table <- function(dsin, row_limit = 10) {
270271
),
271272
collapse = "\n"
272273
)
273-
return(msg)
274+
msg
274275
}

tests/testthat/_snaps/miscellaneous.md

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,159 @@
305305
306306
307307

308+
# `as_ascii_table() can handle missing datetimes (#132)
309+
310+
Code
311+
diffdf(d1, d2, suppress_warnings = TRUE)
312+
Output
313+
Differences found between the objects!
314+
315+
Summary of BASE and COMPARE
316+
==================================================================
317+
PROPERTY BASE COMP
318+
------------------------------------------------------------------
319+
Name d1 d2
320+
Class "tbl_df, tbl, data.frame" "tbl_df, tbl, data.frame"
321+
Rows(#) 3 3
322+
Columns(#) 2 2
323+
------------------------------------------------------------------
324+
325+
326+
Not all Values Compared Equal
327+
=============================
328+
Variable No of Differences
329+
-----------------------------
330+
id 1
331+
dt1 1
332+
-----------------------------
333+
334+
335+
========================================
336+
VARIABLE ..ROWNUMBER.. BASE COMPARE
337+
----------------------------------------
338+
id 2 2 NA
339+
----------------------------------------
340+
341+
342+
===========================================================
343+
VARIABLE ..ROWNUMBER.. BASE COMPARE
344+
-----------------------------------------------------------
345+
dt1 2 2024-01-24 14:12:49 UTC <NA>
346+
-----------------------------------------------------------
347+
348+
349+
350+
---
351+
352+
Code
353+
diffdf(d1, d3, suppress_warnings = TRUE)
354+
Output
355+
Differences found between the objects!
356+
357+
Summary of BASE and COMPARE
358+
==================================================================
359+
PROPERTY BASE COMP
360+
------------------------------------------------------------------
361+
Name d1 d3
362+
Class "tbl_df, tbl, data.frame" "tbl_df, tbl, data.frame"
363+
Rows(#) 3 3
364+
Columns(#) 2 2
365+
------------------------------------------------------------------
366+
367+
368+
Not all Values Compared Equal
369+
=============================
370+
Variable No of Differences
371+
-----------------------------
372+
dt1 3
373+
-----------------------------
374+
375+
376+
===========================================================
377+
VARIABLE ..ROWNUMBER.. BASE COMPARE
378+
-----------------------------------------------------------
379+
dt1 1 2024-01-10 01:02:03 UTC <NA>
380+
dt1 2 2024-01-24 14:12:49 UTC <NA>
381+
dt1 3 1821-02-01 01:01:01 UTC <NA>
382+
-----------------------------------------------------------
383+
384+
385+
386+
# `as_ascii_table() can handle missing dates (#132)
387+
388+
Code
389+
diffdf(d1, d2, suppress_warnings = TRUE)
390+
Output
391+
Differences found between the objects!
392+
393+
Summary of BASE and COMPARE
394+
==================================================================
395+
PROPERTY BASE COMP
396+
------------------------------------------------------------------
397+
Name d1 d2
398+
Class "tbl_df, tbl, data.frame" "tbl_df, tbl, data.frame"
399+
Rows(#) 3 3
400+
Columns(#) 2 2
401+
------------------------------------------------------------------
402+
403+
404+
Not all Values Compared Equal
405+
=============================
406+
Variable No of Differences
407+
-----------------------------
408+
id 1
409+
dt1 1
410+
-----------------------------
411+
412+
413+
========================================
414+
VARIABLE ..ROWNUMBER.. BASE COMPARE
415+
----------------------------------------
416+
id 2 2 NA
417+
----------------------------------------
418+
419+
420+
==============================================
421+
VARIABLE ..ROWNUMBER.. BASE COMPARE
422+
----------------------------------------------
423+
dt1 2 2024-01-24 <NA>
424+
----------------------------------------------
425+
426+
427+
428+
---
429+
430+
Code
431+
diffdf(d1, d3, suppress_warnings = TRUE)
432+
Output
433+
Differences found between the objects!
434+
435+
Summary of BASE and COMPARE
436+
==================================================================
437+
PROPERTY BASE COMP
438+
------------------------------------------------------------------
439+
Name d1 d3
440+
Class "tbl_df, tbl, data.frame" "tbl_df, tbl, data.frame"
441+
Rows(#) 3 3
442+
Columns(#) 2 2
443+
------------------------------------------------------------------
444+
445+
446+
Not all Values Compared Equal
447+
=============================
448+
Variable No of Differences
449+
-----------------------------
450+
dt1 3
451+
-----------------------------
452+
453+
454+
==============================================
455+
VARIABLE ..ROWNUMBER.. BASE COMPARE
456+
----------------------------------------------
457+
dt1 1 2024-01-10 <NA>
458+
dt1 2 2024-01-24 <NA>
459+
dt1 3 1821-02-01 <NA>
460+
----------------------------------------------
461+
462+
463+

tests/testthat/test-miscellaneous.R

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,87 @@ test_that("datetimes compare as expected", {
252252
print(res)
253253
)
254254
})
255+
256+
257+
258+
259+
test_that("`as_ascii_table() can handle missing datetimes (#132)", {
260+
d1 <- tibble(
261+
id = c(1, 2, 3),
262+
dt1 = lubridate::ymd_hms(
263+
"2024-01-10 01-02-03",
264+
"2024-01-24 14-12-49",
265+
"1821-02-01 01-01-01"
266+
)
267+
)
268+
269+
d2 <- tibble(
270+
id = c(1, NA, 3),
271+
dt1 = lubridate::ymd_hms(
272+
"2024-01-10 01-02-03",
273+
NA,
274+
"1821-02-01 01-01-01"
275+
)
276+
)
277+
expect_snapshot(diffdf(d1, d2, suppress_warnings = TRUE))
278+
279+
280+
281+
d3 <- tibble(
282+
id = c(1, 2, 3),
283+
dt1 = lubridate::ymd_hms(NA, NA, NA)
284+
)
285+
expect_snapshot(diffdf(d1, d3, suppress_warnings = TRUE))
286+
287+
288+
set.seed(2211)
289+
offset <- 2000000000
290+
n <- 50
291+
d5 <- tibble(
292+
id = seq_len(n),
293+
dt1 = lubridate::ymd_hms("2000-01-01 01-01-01") + round(runif(n, -offset, offset))
294+
)
295+
d6 <- d5
296+
d6[seq(1, n, by = 2), "dt1"] <- NA
297+
expect_snapshot(diffdf(d5, d6, suppress_warnings = TRUE))
298+
})
299+
300+
301+
test_that("`as_ascii_table() can handle missing dates (#132)", {
302+
d1 <- tibble(
303+
id = c(1, 2, 3),
304+
dt1 = lubridate::ymd(
305+
"2024-01-10",
306+
"2024-01-24",
307+
"1821-02-01"
308+
)
309+
)
310+
311+
d2 <- tibble(
312+
id = c(1, NA, 3),
313+
dt1 = lubridate::ymd(
314+
"2024-01-10",
315+
NA,
316+
"1821-02-01"
317+
)
318+
)
319+
expect_snapshot(diffdf(d1, d2, suppress_warnings = TRUE))
320+
321+
d3 <- tibble(
322+
id = c(1, 2, 3),
323+
dt1 = lubridate::ymd(NA, NA, NA)
324+
)
325+
expect_snapshot(diffdf(d1, d3, suppress_warnings = TRUE))
326+
327+
328+
set.seed(2211)
329+
offset <- 200000
330+
n <- 50
331+
d5 <- tibble(
332+
id = seq_len(n),
333+
dt1 = lubridate::ymd("2000-01-01") + round(runif(n, -offset, offset))
334+
)
335+
d6 <- d5
336+
d6[seq(1, n, by = 2), "dt1"] <- NA
337+
expect_snapshot(diffdf(d5, d6, suppress_warnings = TRUE))
338+
})

0 commit comments

Comments
 (0)