Skip to content

Instantly share code, notes, and snippets.

@tanho63
Created December 9, 2025 01:54
Show Gist options
  • Select an option

  • Save tanho63/29daf3803c2339d2eb65101505cbdd31 to your computer and use it in GitHub Desktop.

Select an option

Save tanho63/29daf3803c2339d2eb65101505cbdd31 to your computer and use it in GitHub Desktop.
updating tidymodel-xgboost for newest version
# install new xgb library to main install loc
pak::pkg_install("xgboost")
# install old xgb library to temporary location
old_xgb_lib <- "~/temp_xgb_lib"
pak::pkg_install("cran/[email protected]", lib = old_xgb_lib, dependencies = FALSE)
update_xgb <- function(local_file, old_xgb_lib = "~/temp_xgb_lib") {
# unload any loaded version of xgboost
unloadNamespace("xgboost")
# load xgboost 1.7
loadNamespace("xgboost", lib = old_xgb_lib)
m <- readRDS(local_file)
# since we use a butchered tidymodel object, the xgb bit is stored here.
# extract the fit and save as a temporary object in a format that new xgboost can use
m$fit$fit$fit |>
xgboost::xgb.Booster.complete() |>
xgboost::xgb.save("local/xgb.json")
# now doctor the original rds file with the modified fit
# first, unload old xgboost
unloadNamespace("xgboost")
# now load new xgboost
loadNamespace("xgboost", lib = .libPaths()[1])
# load model and assign to correct spot in old model object
m$fit$fit$fit <- xgboost::xgb.load("local/xgb.json")
# overwrite the old model object with the new one
saveRDS(m, local_file)
return(local_file)
}
# repeat for every model object path
sapply(local_model_files, update_xgb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment