Created
December 9, 2025 01:54
-
-
Save tanho63/29daf3803c2339d2eb65101505cbdd31 to your computer and use it in GitHub Desktop.
updating tidymodel-xgboost for newest version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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