Debian Codegraph @

R/xgb.config.R

raw
1 #' Set and get global configuration
2 #'
3 #' Global configuration consists of a collection of parameters that can be applied in the global
4 #' scope. See \url{https://xgboost.readthedocs.io/en/stable/parameter.html} for the full list of
5 #' parameters supported in the global configuration. Use `xgb.set.config()` to update the
6 #' values of one or more global-scope parameters. Use `xgb.get.config()` to fetch the current
7 #' values of all global-scope parameters (listed in
8 #' \url{https://xgboost.readthedocs.io/en/stable/parameter.html}).
9 #'
10 #' @details
11 #' Note that serialization-related functions might use a globally-configured number of threads,
12 #' which is managed by the system's OpenMP (OMP) configuration instead. Typically, XGBoost methods
13 #' accept an `nthreads` parameter, but some methods like [readRDS()] might get executed before such
14 #' parameter can be supplied.
15 #'
16 #' The number of OMP threads can in turn be configured for example through an environment variable
17 #' `OMP_NUM_THREADS` (needs to be set before R is started), or through `RhpcBLASctl::omp_set_num_threads`.
18 #' @rdname xgbConfig
19 #' @name xgb.set.config, xgb.get.config
20 #' @export xgb.set.config xgb.get.config
21 #' @param ... List of parameters to be set, as keyword arguments
22 #' @return
23 #' `xgb.set.config()` returns `TRUE` to signal success. `xgb.get.config()` returns
24 #' a list containing all global-scope parameters and their values.
25 #'
26 #' @examples
27 #' # Set verbosity level to silent (0)
28 #' xgb.set.config(verbosity = 0)
29 #' # Now global verbosity level is 0
30 #' config <- xgb.get.config()
31 #' print(config$verbosity)
32 #' # Set verbosity level to warning (1)
33 #' xgb.set.config(verbosity = 1)
34 #' # Now global verbosity level is 1
35 #' config <- xgb.get.config()
36 #' print(config$verbosity)
37 xgb.set.config <- function(...) {
38 new_config <- list(...)
39 .Call(XGBSetGlobalConfig_R, jsonlite::toJSON(new_config, auto_unbox = TRUE))
40 return(TRUE)
41 }
42
43 #' @rdname xgbConfig
44 xgb.get.config <- function() {
45 config <- .Call(XGBGetGlobalConfig_R)
46 return(jsonlite::fromJSON(config))
47 }