Skip to content

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.

Usage

# S3 method for step_umap
bundle(x, ...)

Arguments

x

A step_umap object returned from embed.

...

Not used in this bundler and included for compatibility with the generic only. Additional arguments passed to this method will return an error.

Value

A bundle object with subclass bundled_step_umap.

Bundles are a list subclass with two components:

object

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.

situate

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 object component. Calling unbundle() on a bundled object x calls x$situate(x$object), returning the unserialized version of object. 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 with 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 bundle with base::readRDS() and run unbundle() on it. The output of 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.

See vignette("bundle") for more information on bundling and its motivation.

bundle and butcher

The butcher package allows you to remove parts of a fitted model object that are not needed for prediction.

This bundle method is compatible with pre-butchering. That is, for a fitted model x, you can safely call:

res <-
  x %>%
  butcher() %>%
  bundle()

and predict with the output of unbundle(res) in a new R session.

Examples

# fit model and bundle ------------------------------------------------
library(recipes)
#> Loading required package: dplyr
#> 
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#> 
#>     filter, lag
#> The following objects are masked from ‘package:base’:
#> 
#>     intersect, setdiff, setequal, union
#> 
#> Attaching package: ‘recipes’
#> The following object is masked from ‘package:stats’:
#> 
#>     step
library(embed)

set.seed(1)

rec <- recipe(Species ~ ., data = iris) %>%
  step_normalize(all_predictors()) %>%
  step_umap(all_predictors(), outcome = vars(Species), num_comp = 2) %>%
  prep()

rec_bundle <- bundle(rec)

# then, after saveRDS + readRDS or passing to a new session ----------
rec_unbundled <- unbundle(rec_bundle)

bake(rec_unbundled, new_data = iris)
#> # A tibble: 150 × 3
#>    Species UMAP1  UMAP2
#>    <fct>   <dbl>  <dbl>
#>  1 setosa  -5.50 -1.10 
#>  2 setosa  -6.34  1.17 
#>  3 setosa  -6.90  0.376
#>  4 setosa  -6.37  0.506
#>  5 setosa  -6.53 -1.32 
#>  6 setosa  -6.20 -2.30 
#>  7 setosa  -6.45 -0.277
#>  8 setosa  -5.60 -0.631
#>  9 setosa  -7.18  1.11 
#> 10 setosa  -6.21  1.16 
#> # … with 140 more rows