Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
36 changes: 31 additions & 5 deletions R/fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,35 @@ CmdStanFit <- R6::R6Class(
)
)

#' Materialize model object
#'
#' @name fit-method-materialize
#' @aliases materialize
#' @description This method collects all posterior draws and diagnostics of a fitted
#' model object into R, since the contents of the CmdStan output CSV files are only
#' read into R lazily (i.e., as needed).
#'
#'
#' @seealso [`save_object`]
#'
#' @examples
#' \dontrun{
#' fit <- cmdstanr_example("logistic")
#' object.size(fit)
#'
#' fit$materialize()
#' object.size(fit)
#' }
#'
materialize <- function() {
self$draws()
try(self$sampler_diagnostics(), silent = TRUE)
try(self$init(), silent = TRUE)
try(self$profiles(), silent = TRUE)
invisible(self)
}
CmdStanFit$set("public", name = "materialize", value = materialize)

#' Save fitted model object to a file
#'
#' @name fit-method-save_object
Expand All @@ -123,7 +152,7 @@ CmdStanFit <- R6::R6Class(
#' @param ... Other arguments to pass to [base::saveRDS()] (for `format = "rds"`)
#' or `qs2::qs_save()` (for `format = "qs2"`).
#'
#' @seealso [`CmdStanMCMC`], [`CmdStanMLE`], [`CmdStanVB`], [`CmdStanGQ`]
#' @seealso [`CmdStanMCMC`], [`CmdStanMLE`], [`CmdStanVB`], [`CmdStanGQ`], [`materialize`]
#'
#' @examples
#' \dontrun{
Expand All @@ -138,10 +167,7 @@ CmdStanFit <- R6::R6Class(
#' }
#'
save_object <- function(file, format = c("rds", "qs2"), ...) {
self$draws()
try(self$sampler_diagnostics(), silent = TRUE)
try(self$init(), silent = TRUE)
try(self$profiles(), silent = TRUE)
self$materialize()
format <- match.arg(format)
if (format == "rds") {
saveRDS(self, file = file, ...)
Expand Down
27 changes: 27 additions & 0 deletions man/fit-method-materialize.Rd

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

2 changes: 1 addition & 1 deletion man/fit-method-save_object.Rd

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

Loading