Bundle a tidymodels
Bundling a model prepares it to be saved to a file and later restored for prediction in a new R session. See the 'Value' section for more information on bundles and their usage.
# S3 method for model_stack bundle(x, ...)
A bundle object with subclass
Bundles are a list subclass with two components:
An R object. Gives the output of native serialization methods from the model-supplying package, sometimes with additional classes or attributes that aid portability. This is often a raw object.
A function. The
situate()function is defined when
bundle()is called, though is a loose analogue of an
unbundle()S3 method for that object. Since the function is defined on
bundle(), it has access to references and dependency information that can be saved alongside the
unbundle()on a bundled object
x$situate(x$object), returning the unserialized version of
situate()will also restore needed references, such as server instances and environmental variables.
Bundles are R objects that represent a "standalone" version of their
analogous model object. Thus, bundles are ready for saving to a file; saving
base::saveRDS() is our recommended serialization strategy for bundles,
unless documented otherwise for a specific method.
To restore the original model object
x in a new environment, load its
base::readRDS() and run
unbundle() on it. The output
unbundle() is a model object that is ready to
predict() on new data,
and other restored functionality (like plotting or summarizing) is supported
as a side effect only.
The bundle package wraps native serialization methods from model-supplying packages. Between versions, those model-supplying packages may change their native serialization methods, possibly introducing problems with re-loading objects serialized with previous package versions. The bundle package does not provide checks for these sorts of changes, and ought to be used in conjunction with tooling for managing and monitoring model environments like vetiver or renv.
vignette("bundle") for more information on bundling and its motivation.
# fit model and bundle ------------------------------------------------ library(stacks) set.seed(1) mod <- stacks() %>% add_candidates(reg_res_lr) %>% add_candidates(reg_res_svm) %>% blend_predictions(times = 10) %>% fit_members() mod_bundle <- bundle(mod) # then, after saveRDS + readRDS or passing to a new session ---------- mod_unbundled <- unbundle(mod_bundle)