Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

373 slices_restore will restore Dates and POSIXT classes #432

Merged
merged 27 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
09ffb6f
add dates and times restore on slice_restore
m7pr Aug 9, 2023
ca6a7fd
Merge 09ffb6f1283ca1b6d2db8d3eafe9e1acd623b468 into cf44c48feae4b314c…
m7pr Aug 9, 2023
d09db50
[skip actions] Restyle files
github-actions[bot] Aug 9, 2023
52df52f
unify storage timezones to UTC
m7pr Aug 9, 2023
716f0cc
merge
m7pr Aug 9, 2023
c915a3d
space
m7pr Aug 9, 2023
7c2873e
#373 convert dates/posixt to ISO standard
m7pr Sep 4, 2023
7a6acf1
Merge branch 'main' into 373_restore_classes@main
m7pr Sep 4, 2023
d4c1877
#373 remove time startpoint and endpoint at the restore
m7pr Sep 4, 2023
9bf3e70
Update R/teal_slice-store.R
m7pr Sep 13, 2023
6881e57
Merge branch 'main' into 373_restore_classes@main
m7pr Sep 13, 2023
d88262c
373 adds tests to slices_restore (#465)
m7pr Sep 13, 2023
d7aebc9
#373 extend dates/times class in restore for choices and selected
m7pr Sep 13, 2023
698d865
Merge branch 'main' into 373_restore_classes@main
m7pr Sep 21, 2023
98e6b56
ensure dates mixed in a character are not returned as Dates
m7pr Sep 25, 2023
fc855c3
superlintr fixes
m7pr Sep 25, 2023
c288b7c
#467 change factors to characters for selected and choices at teal_sl…
m7pr Sep 29, 2023
b68af39
Empty-Commit
m7pr Sep 29, 2023
b6db13d
fix testthat:: for teal_slice factor conversion
m7pr Sep 29, 2023
2d246e0
format appends timezone to POSIX classes, restores assumes timezone i…
m7pr Oct 17, 2023
1c2c2a4
Merge branch 'main' into 373_restore_classes@main
m7pr Oct 17, 2023
cd218bf
change POSIXct to POXIXlt in slices_restore for POSIX classes /timest…
m7pr Oct 17, 2023
38cc8f9
Merge branch '373_restore_classes@main' of https://github.com/insight…
m7pr Oct 17, 2023
55781e9
try format = "%Y-%m-%d %H:%M:%S %z", usetz = FALSE
m7pr Oct 18, 2023
df99d7e
convert all POSIX*t in teal_slice(s) to UTC in print and format. use …
m7pr Oct 18, 2023
d41e446
update final regex assuming timestamp are stored in UTC
m7pr Oct 18, 2023
7f0020c
append origin to as.POSIXct.numeric tests
m7pr Oct 18, 2023
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
20 changes: 20 additions & 0 deletions R/teal_slice-store.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#' @param file (`character(1)`) The file path where `teal_slices` object will be saved.
#' The file extension should be `".json"`.
#'
#' @details `Date` and `POSIXt` classes are stored in `"ISO8601"` format and are converted to `UTC` timezone.
#'
#' @return `NULL`, invisibly.
#'
#' @examples
Expand Down Expand Up @@ -51,6 +53,24 @@ slices_restore <- function(file) {
checkmate::assert_file_exists(file, access = "r", extension = "json")

tss_json <- jsonlite::fromJSON(file, simplifyDataFrame = FALSE)
tss_json$slices <-
lapply(tss_json$slices, function(slice) {
for (field in c("selected", "choices")) {
if (!is.null(slice[[field]])) {
slice[[field]] <-
if (all(grepl("[0-9]{4}-[0-9]{2}-[0-9]{2}", slice[[field]]))) {
if (all(grepl("T[0-9]{2}:[0-9]{2}:[0-9]{2}", slice[[field]]))) {
as.POSIXct(gsub("T|Z", " ", slice[[field]]), tz = "UTC")
} else {
as.Date(slice[[field]])
}
} else {
slice[[field]]
}
}
}
slice
})

tss_elements <- lapply(tss_json$slices, as.teal_slice)

Expand Down
2 changes: 1 addition & 1 deletion R/teal_slice.R
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ to_json <- function(x) {
}
}

jsonlite::toJSON(no_unbox(x), pretty = TRUE, auto_unbox = TRUE, digits = 16, null = "null")
jsonlite::toJSON(no_unbox(x), pretty = TRUE, auto_unbox = TRUE, digits = 16, null = "null", UTC = TRUE, POSIXt = "ISO8601")
}

#' Justify Colons in `JSON` String
Expand Down
3 changes: 3 additions & 0 deletions man/slices_store.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

109 changes: 109 additions & 0 deletions tests/testthat/test-teal_slice-store.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
test_that("teal_slice store/restore supports saving `POSIXct` timestamps in selected", {
slices_path <- withr::local_file("slices.json")

time_stamps <- Sys.time() + c(-10 * 60 * 60 * 24, -30, 0)

# ISO8601 does not keep milliseconds
time_stamps <- as.POSIXct(
ceiling(as.double(time_stamps)),
tz = "UTC"
)

tss <- teal_slices(
teal_slice(
dataname = "ADSL",
varname = "EOSDTM",
selected = time_stamps,
fixed = TRUE
)
)

# Store the teal_slices object to a file
slices_store(tss, slices_path)
tss_restored <- slices_restore(slices_path)

tss_restored_list <- shiny::isolate(shiny::reactiveValuesToList(tss_restored[[1]]))
expect_s3_class(tss_restored_list$selected, "POSIXct")

expect_identical_slice(tss[[1]], tss_restored[[1]])
})

test_that("teal_slice store/restore supports saving `Date` dates in selected", {
slices_path <- withr::local_file("slices.json")

time_stamps <- Sys.Date() + c(-10 * 600, -30, 0)

tss <- teal_slices(
teal_slice(
dataname = "ADSL",
varname = "EOSDT",
selected = time_stamps,
fixed = TRUE
)
)

# Store the teal_slices object to a file
slices_store(tss, slices_path)
tss_restored <- slices_restore(slices_path)

tss_restored_list <- shiny::isolate(shiny::reactiveValuesToList(tss_restored[[1]]))
expect_s3_class(tss_restored_list$selected, "Date")

expect_identical_slice(tss[[1]], tss_restored[[1]])
})

test_that("teal_slice store/restore supports saving `POSIXct` timestamps in choices", {
slices_path <- withr::local_file("slices.json")

time_stamps <- Sys.time() + c(-10 * 60 * 60 * 24, -30, 0)

# ISO8601 does not keep milliseconds
time_stamps <- as.POSIXct(
ceiling(as.double(time_stamps)),
tz = "UTC"
)

tss <- teal_slices(
teal_slice(
dataname = "ADSL",
varname = "EOSDTM",
selected = sample(time_stamps, 2),
choices = time_stamps,
fixed = TRUE
)
)

# Store the teal_slices object to a file
slices_store(tss, slices_path)
tss_restored <- slices_restore(slices_path)

tss_restored_list <- shiny::isolate(shiny::reactiveValuesToList(tss_restored[[1]]))
expect_s3_class(tss_restored_list$choices, "POSIXct")

expect_identical_slice(tss[[1]], tss_restored[[1]])
})

test_that("teal_slice store/restore supports saving `Date` timestamps in choices", {
slices_path <- withr::local_file("slices.json")

time_stamps <- Sys.Date() + c(-10 * 600, -30, 0)

tss <- teal_slices(
teal_slice(
dataname = "ADSL",
varname = "EOSDT",
selected = sample(time_stamps, 2),
choices = time_stamps,
fixed = TRUE
)
)

# Store the teal_slices object to a file
slices_store(tss, slices_path)
tss_restored <- slices_restore(slices_path)

tss_restored_list <- shiny::isolate(shiny::reactiveValuesToList(tss_restored[[1]]))
expect_s3_class(tss_restored_list$choices, "Date")

expect_identical_slice(tss[[1]], tss_restored[[1]])
})