I am aware that the in Python you can serialize a ML model using the pickle module; however, is there a method to do something similar in the tidymodel space? My goal would be to be able to save a trained model to be deployed later.

2

There are 2 best solutions below

2
On BEST ANSWER

In R, you can use saveRDS & readRDS to save/load any R object, just like Python's pickle. Those functions are not specific to Tidymodels, they are basic R functions that can be used to serialize any object.

Usage

saveRDS(any_r_object, "filename.rds")  
object_name <- readRDS("filename.rds")

There is also the save() & load() functions, they serve the same function are are mostly similar to saveRDS() & readRDS(). There are many online discussions/blogs comparing the two approaches.

0
On

Following up after a good while:

Some model fits in R, e.g. those fitted with the "xgboost" or "keras" engine in tidymodels, require native serialization methods to be saved and reloaded in a new R session properly. saveRDS and readRDS will do the trick most of the time, though fall short for model objects from packages that require native serialization.

The folks from the tidymodels team put together a new package, bundle, to provide a consistent interface for native serialization of model objects. The bundle() verb prepares a model object for serialization, and then you can safely saveRDS() + readRDS() and pass between R sessions as you wish, and then unbundle() in the new session:

mod_bundle <- bundle(mod)
saveRDS(mod_bundle, file = "path/to/file.rds")

# in a new R session:
mod_bundle <- readRDS("path/to/file.rds")
mod_new <- unbundle(mod_bundle)