Skip to content

Commit

Permalink
add auto-converter for POSIXct
Browse files Browse the repository at this point in the history
  • Loading branch information
mllg committed Jan 20, 2021
1 parent 43a4269 commit aad7fea
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
12 changes: 12 additions & 0 deletions R/auto_convert.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ ee[["logical___factor"]] =
function(value, type, levels) { factor(value, levels = union(levels, value), ordered = FALSE) }
ee[["logical___ordered"]] =
function(value, type, levels) { if (all(value %in% c(NA_character_, levels))) factor(value, levels = levels, ordered = TRUE) else NULL }
ee[["logical___POSIXct"]] =
function(value, type, levels) { if (allMissing(value)) .POSIXct(value, tz = "") else value }

## from: integer
ee[["integer___logical"]] =
Expand All @@ -26,6 +28,8 @@ ee[["integer___factor"]] =
ee[["logical___factor"]]
ee[["integer___ordered"]] =
ee[["logical___ordered"]]
ee[["integer___POSIXct"]] =
ee[["logical___POSIXct"]]

## from: numeric
ee[["numeric___logical"]] =
Expand All @@ -38,6 +42,8 @@ ee[["numeric___factor"]] =
ee[["logical___factor"]]
ee[["numeric___ordered"]] =
ee[["logical___ordered"]]
ee[["numeric___POSIXct"]] =
ee[["logical___POSIXct"]]

## from: character
ee[["character___logical"]] =
Expand All @@ -50,6 +56,8 @@ ee[["character___factor"]] =
ee[["logical___factor"]]
ee[["character___ordered"]] =
ee[["logical___ordered"]]
ee[["character___POSIXct"]] =
function(value, type, levels) { x = try(as.POSIXct(value), silent = TRUE); if (inherits(x, "try-error")) value else x }

## from: factor
ee[["factor___logical"]] =
Expand All @@ -60,6 +68,8 @@ ee[["factor___factor"]] =
function(value, type, levels) { factor(value, levels = union(levels, levels(value)), ordered = FALSE) }
ee[["factor___ordered"]] =
function(value, type, levels) { if (all(levels(value) %in% levels)) factor(value, levels = levels, ordered = TRUE) else NULL }
ee[["factor___POSIXct"]] =
ee[["character___POSIXct"]]

## from: ordered
ee[["ordered___character"]] =
Expand All @@ -68,6 +78,8 @@ ee[["ordered___factor"]] =
ee[["factor___factor"]]
ee[["ordered___ordered"]] =
ee[["ordered___ordered"]]
ee[["ordered___POSIXct"]] =
ee[["character___POSIXct"]]

rm(ee)
# nolint end
Expand Down
32 changes: 32 additions & 0 deletions tests/testthat/test_auto_convert.R
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,35 @@ test_that("ordered", {
ordered(c("1", NA, "0"), levels = c("1", "0"))
)
})

test_that("POSIXct", {
expect_identical(
auto_convert(NA, "x", "POSIXct", character()),
.POSIXct(NA, "")
)

expect_identical(
auto_convert(NA_integer_, "x", "POSIXct", character()),
.POSIXct(NA, "")
)

expect_identical(
auto_convert(NA_real_, "x", "POSIXct", character()),
.POSIXct(NA_real_, "")
)

expect_identical(
auto_convert(factor(NA_character_, levels = "a"), "x", "POSIXct", "a"),
.POSIXct(NA, "")
)

expect_identical(
auto_convert(ordered(NA_character_, levels = "a"), "x", "POSIXct", "a"),
.POSIXct(NA, "")
)

expect_identical(
auto_convert("2020-01-20 10:00:00", "x", "POSIXct", character()),
as.POSIXct("2020-01-20 10:00:00", "")
)
})

0 comments on commit aad7fea

Please sign in to comment.