Skip to content

Commit

Permalink
Using ParallelLogger's JSON functions to store attributes. Fixes #54
Browse files Browse the repository at this point in the history
  • Loading branch information
Admin_mschuemi authored and Admin_mschuemi committed Mar 29, 2023
1 parent 09d73a0 commit 33b6872
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ Imports:
pillar,
glue,
tidyselect,
jsonlite
jsonlite,
ParallelLogger
Suggests:
testthat,
dbplyr,
Expand Down
6 changes: 4 additions & 2 deletions R/LoadingSaving.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ saveAndromeda <- function(andromeda, fileName, overwrite = TRUE) {
attribs <- attributes(andromeda)
attribs[["class"]] <- attribs[["path"]] <- attribs[["names"]] <- attribs[["env"]] <- attribs[[".xData"]] <- NULL
attributesFileName <- file.path(tempdir(), "user-defined-attributes.json")
jsonlite::write_json(attribs, attributesFileName)
ParallelLogger::saveSettingsToJson(attribs, attributesFileName)
# jsonlite::write_json(attribs, attributesFileName, pretty = TRUE, force = TRUE, null = "null", auto_unbox = TRUE)

tableDirs <- list.dirs(attr(andromeda, "path"), recursive = FALSE)
zip::zipr(fileName, c(attributesFileName, tableDirs), compression_level = 2)
Expand Down Expand Up @@ -115,7 +116,8 @@ loadAndromeda <- function(fileName) {
}
class(andr) <- andrClass

attributes <- jsonlite::read_json(file.path(path, "user-defined-attributes.json"), simplifyVector = TRUE)
attributes <- ParallelLogger::loadSettingsFromJson(file.path(path, "user-defined-attributes.json"))
# attributes <- jsonlite::read_json(file.path(path, "user-defined-attributes.json"), simplifyVector = TRUE)
on.exit(unlink(file.path(path, "user-defined-attributes.json")))
for (nm in names(attributes)) {
attr(andr, nm) <- attributes[[nm]]
Expand Down
5 changes: 4 additions & 1 deletion tests/testthat/test-loadingSaving.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ test_that("Saving and loading", {
expect_true("table" %in% names(andromeda))
iris1 <- andromeda$table %>% collect()

attr(andromeda, "metaData") <- list(x = 1)
s3Object <- list(a = 1)
class(s3Object) <- "MyClass"
attr(andromeda, "metaData") <- list(x = 1, y = list(s3Object))

fileName <- tempfile(fileext = ".zip")
fileName <- file.path(.getAndromedaTempFolder(), "asdf.zip")
Expand All @@ -27,6 +29,7 @@ test_that("Saving and loading", {
expect_equivalent(iris1, iris2)
expect_false(is.null(attr(andromeda2, "metaData")))
expect_equal(attr(andromeda2, "metaData")$x, 1)
expect_equal(class(attr(andromeda2, "metaData")$y[[1]]), "MyClass")

expect_error(capture.output(saveAndromeda(andromeda2, fileName, overwrite = TRUE)), NA)

Expand Down

0 comments on commit 33b6872

Please sign in to comment.