Skip to content

Commit a9fcfa7

Browse files
authored
Merge pull request #1181 from MansMeg/add-materialize-method
add materialize() method
2 parents cb6d218 + 1fac734 commit a9fcfa7

3 files changed

Lines changed: 59 additions & 6 deletions

File tree

R/fit.R

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,35 @@ CmdStanFit <- R6::R6Class(
9999
)
100100
)
101101

102+
#' Materialize model object
103+
#'
104+
#' @name fit-method-materialize
105+
#' @aliases materialize
106+
#' @description This method collects all posterior draws and diagnostics of a fitted
107+
#' model object into R, since the contents of the CmdStan output CSV files are only
108+
#' read into R lazily (i.e., as needed).
109+
#'
110+
#'
111+
#' @seealso [`save_object`]
112+
#'
113+
#' @examples
114+
#' \dontrun{
115+
#' fit <- cmdstanr_example("logistic")
116+
#' object.size(fit)
117+
#'
118+
#' fit$materialize()
119+
#' object.size(fit)
120+
#' }
121+
#'
122+
materialize <- function() {
123+
self$draws()
124+
try(self$sampler_diagnostics(), silent = TRUE)
125+
try(self$init(), silent = TRUE)
126+
try(self$profiles(), silent = TRUE)
127+
invisible(self)
128+
}
129+
CmdStanFit$set("public", name = "materialize", value = materialize)
130+
102131
#' Save fitted model object to a file
103132
#'
104133
#' @name fit-method-save_object
@@ -123,7 +152,7 @@ CmdStanFit <- R6::R6Class(
123152
#' @param ... Other arguments to pass to [base::saveRDS()] (for `format = "rds"`)
124153
#' or `qs2::qs_save()` (for `format = "qs2"`).
125154
#'
126-
#' @seealso [`CmdStanMCMC`], [`CmdStanMLE`], [`CmdStanVB`], [`CmdStanGQ`]
155+
#' @seealso [`CmdStanMCMC`], [`CmdStanMLE`], [`CmdStanVB`], [`CmdStanGQ`], [`materialize`]
127156
#'
128157
#' @examples
129158
#' \dontrun{
@@ -138,10 +167,7 @@ CmdStanFit <- R6::R6Class(
138167
#' }
139168
#'
140169
save_object <- function(file, format = c("rds", "qs2"), ...) {
141-
self$draws()
142-
try(self$sampler_diagnostics(), silent = TRUE)
143-
try(self$init(), silent = TRUE)
144-
try(self$profiles(), silent = TRUE)
170+
self$materialize()
145171
format <- match.arg(format)
146172
if (format == "rds") {
147173
saveRDS(self, file = file, ...)

man/fit-method-materialize.Rd

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/fit-method-save_object.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)