Skip to content

Commit

Permalink
Add a default marshal() method [#7]
Browse files Browse the repository at this point in the history
  • Loading branch information
BHGC Website GHA Workflow Runner committed Dec 14, 2023
1 parent 47e08ec commit 72e976c
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: marshal
Version: 0.0.1-9002
Version: 0.0.1-9003
Title: Framework to Marshal Objects to be Used in Another R Process
Depends:
R (>= 3.2.0)
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ S3method(marshal,XMLAbstractDocument)
S3method(marshal,XMLAbstractNode)
S3method(marshal,connection)
S3method(marshal,data.table)
S3method(marshal,default)
S3method(marshal,jclassName)
S3method(marshal,jobjRef)
S3method(marshal,keras.engine.base_layer.Layer)
Expand Down
1 change: 1 addition & 0 deletions R/marshal.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#' @export
marshal <- function(...) { UseMethod("marshal") }


#' @return
#' `unmarshal()` returns an unmarshalled version of the original object.
#'
Expand Down
40 changes: 40 additions & 0 deletions R/marshal.default.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#' Marshalling of an R objects
#'
#' @param x
#' An R object.
#'
#' @param \dots Not used.
#'
#' @return
#' A `marshalled` object as described in [marshal()].
#'
#' @details
#' This is the default, fallback method for marshalling of an
#' \R object. It assumes that the object do not need specific
#' marshalling, but can be serialized as-is. All this default
#' method does is wrapping the original object up in a
#' `marshalled` object, so that the original object can be
#' recovered via `unmarshalled()`.
#'
#' @example incl/marshal.default.R
#'
#' @rdname marshal.default
#' @aliases marshal.default
#' @export
marshal.default <- function(x, ...) {
res <- list(
marshalled = x
)
class(res) <- marshal_class(x)

res[["unmarshal"]] <- unmarshal_default

res
}


unmarshal_default <- function(x, ...) {
res <- x[["marshalled"]]
stopifnot(all.equal(class(res), marshal_unclass(x), check.attributes = FALSE))
res
}
6 changes: 6 additions & 0 deletions incl/marshal.default.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
x <- data.frame(a = 1:3, b = letters[1:3])
x_ <- marshal(x)
x2 <- unmarshal(x_)
stopifnot(identical(x2, x))


35 changes: 35 additions & 0 deletions man/marshal.default.Rd

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

15 changes: 15 additions & 0 deletions tests/marshal.default.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
library(marshal)

objects <- list(
1:3,
rnorm(3),
as.list(1:3),
data.frame(a = 1:3, b = letters[1:3])
)

for (x in objects) {
x_ <- marshal(x)
x2 <- unmarshal(x_)
stopifnot(identical(x2, x))
}

0 comments on commit 72e976c

Please sign in to comment.