From 614ae1d37ce45987f24c52ee8bc41ab1790944f9 Mon Sep 17 00:00:00 2001 From: Rich FitzJohn Date: Wed, 18 Sep 2024 09:25:20 +0100 Subject: [PATCH] Add additional example for packer --- R/packer.R | 15 +++++++++++++++ man/monty_packer.Rd | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/R/packer.R b/R/packer.R index caaaafb6..6f1deed2 100644 --- a/R/packer.R +++ b/R/packer.R @@ -196,6 +196,21 @@ ##' fixed = list(d = data.frame(n = 1:3, m = runif(3)))) ##' p$unpack(1:2) ##' p$pack(p$unpack(1:2)) +##' +##' # The example from above, where we create a symmetric 2 x 2 matrix +##' # from a 3-element vector, alongside a scalar: +##' p <- monty_packer( +##' scalar = "a", +##' array = list(b_flat = 3), +##' process = function(p) list(b = matrix(p$b_flat[c(1, 2, 2, 3)], 2, 2))) +##' +##' # Unpacking we see "b_flat" is still in the list, but "b" is our +##' # symmetric matrix: +##' p$unpack(1:4) +##' +##' # The processed elements are ignored on the return pack: +##' p$pack(list(a = 1, b_flat = 2:4, b = matrix(c(2, 3, 3, 4), 2, 2))) +##' p$pack(list(a = 1, b_flat = 2:4)) monty_packer <- function(scalar = NULL, array = NULL, fixed = NULL, process = NULL) { call <- environment() diff --git a/man/monty_packer.Rd b/man/monty_packer.Rd index 09302e9a..48b5fcd2 100644 --- a/man/monty_packer.Rd +++ b/man/monty_packer.Rd @@ -199,4 +199,19 @@ p <- monty_packer( fixed = list(d = data.frame(n = 1:3, m = runif(3)))) p$unpack(1:2) p$pack(p$unpack(1:2)) + +# The example from above, where we create a symmetric 2 x 2 matrix +# from a 3-element vector, alongside a scalar: +p <- monty_packer( + scalar = "a", + array = list(b_flat = 3), + process = function(p) list(b = matrix(p$b_flat[c(1, 2, 2, 3)], 2, 2))) + +# Unpacking we see "b_flat" is still in the list, but "b" is our +# symmetric matrix: +p$unpack(1:4) + +# The processed elements are ignored on the return pack: +p$pack(list(a = 1, b_flat = 2:4, b = matrix(c(2, 3, 3, 4), 2, 2))) +p$pack(list(a = 1, b_flat = 2:4)) }