Skip to content

Commit 0a3d318

Browse files
authored
[R] Allow controlling strict mode through env. variable (dmlc#11254)
1 parent 52f1a2a commit 0a3d318

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

R-package/R/utils.R

+14-1
Original file line numberDiff line numberDiff line change
@@ -540,11 +540,24 @@ NULL
540540
#'
541541
#' Currently, the only supported option is `xgboost.strict_mode`, which can be set to `TRUE` or
542542
#' `FALSE` (default).
543+
#'
544+
#' In addition to an R option, it can also be enabled through by setting environment variable
545+
#' `XGB_STRICT_MODE=1`. If set, this environment variable will take precedence over the option.
543546
#' @examples
544547
#' options("xgboost.strict_mode" = FALSE)
545548
#' options("xgboost.strict_mode" = TRUE)
549+
#' Sys.setenv("XGB_STRICT_MODE" = "1")
550+
#' Sys.setenv("XGB_STRICT_MODE" = "0")
546551
NULL
547552

553+
get.strict.mode.option <- function() {
554+
env_var_option <- Sys.getenv("XGB_STRICT_MODE")
555+
if (!nchar(env_var_option)) {
556+
return(getOption("xgboost.strict_mode", default = FALSE))
557+
}
558+
return(tolower(as.character(env_var_option)) %in% c("1", "true", "t", "yes", "y"))
559+
}
560+
548561
# Lookup table for the deprecated parameters bookkeeping
549562
deprecated_train_params <- list(
550563
renamed = list(
@@ -639,7 +652,7 @@ check.deprecation <- function(
639652
if (length(params) == 0) {
640653
return(NULL)
641654
}
642-
error_on_deprecated <- getOption("xgboost.strict_mode", default = FALSE)
655+
error_on_deprecated <- get.strict.mode.option()
643656
throw_err_or_depr_msg <- function(...) {
644657
if (error_on_deprecated) {
645658
stop(...)

R-package/man/xgboost-options.Rd

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

doc/R-package/migration_guide.rst

+19
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,22 @@ XGBoost's R language bindings had large breaking changes between versions 1.x an
4444
- R-side attributes (which can be accessed and modified through R function ``attributes(model)`` and ``attributes(model)$field <- val``), which allow arbitrary objects. Many attributes are automatically added by the model building functions, such as evaluation logs (a ``data.table`` with metrics calculated per iteration), which previously were model fields.
4545
- C-level attributes, which allow only JSON-compliant data and which can be accessed and set through function ``xgb.attributes(model)``. These C-level attributes are shareable through serialized models in different XGBoost interfaces, while the R-level ones are specific to the R interface. Some attributes that are standard among language bindings of XGBoost, such as the best interation, are kept as C attributes.
4646
- Previously, models that were just de-serialized from an on-disk format required calling method 'xgb.Booster.complete' on them to finish the full de-serialization process before being usable, or would otherwise call this method on their own automatically automatically at the first call to 'predict'. Serialization is now handled more gracefully, and there are no additional functions/methods involved - i.e. if one saves a model to disk with ``saveRDS()`` and then reads it back with ``readRDS()``, the model will be fully loaded straight away, without needing to call additional methods on it.
47+
48+
Other recommendations
49+
---------------------
50+
51+
By default, XGBoost might recognize that some parameter has been removed or renamed from how it was in a previous version, and still accept the same function call as it used to do before with the renamed or removed arugments, but issuing a deprecation warning along the way that highlights the changes.
52+
53+
These behaviors will be removed in future versions, and function calls which currently return deprecation warnings will stop working in the future, so in order to make sure that code calling XGBoost will still keep working, it should be ensured that it doesn't issue deprecation warnings.
54+
55+
Optionally, these deprecation warnings can be turned into errors (while still keeping other types of warnings as warnings) through an option "xgboost.strict_mode" - example:
56+
.. code-block:: r
57+
58+
options("xgboost.strict_mode" = TRUE)
59+
60+
It can also be controlled through an environment variable `XGB_STRICT_MODE=1`, which takes precende over the R option - e.g.:
61+
.. code-block:: r
62+
63+
Sys.setenv("XGB_STRICT_MODE" = "1")
64+
65+
It is highly recommended for package developers to enable this option during their package checks to ensure better compatibility with XGBoost.

0 commit comments

Comments
 (0)