lgb.make_serializable.R 1.03 KB
Newer Older
1
2
3
4
5
6
#' @name lgb.make_serializable
#' @title Make a LightGBM object serializable by keeping raw bytes
#' @description If a LightGBM model object was produced with argument `serializable=FALSE`, the R object will not
#' be serializable (e.g. cannot save and load with \code{saveRDS} and \code{readRDS}) as it will lack the raw bytes
#' needed to reconstruct its underlying C++ object. This function can be used to forcibly produce those serialized
#' raw bytes and make the object serializable. Note that the object will be modified in-place.
7
8
9
#'
#'              \emph{New in version 4.0.0}
#'
10
11
12
13
14
15
#' @param model \code{lgb.Booster} object which was produced with `serializable=FALSE`.
#'
#' @return \code{lgb.Booster} (the same `model` object that was passed as input, as invisible).
#' @seealso \link{lgb.restore_handle}, \link{lgb.drop_serialized}.
#' @export
lgb.make_serializable <- function(model) {
16
  if (!.is_Booster(x = model)) {
17
18
19
20
21
    stop("lgb.make_serializable: model should be an ", sQuote("lgb.Booster"))
  }
  model$save_raw()
  return(invisible(model))
}