From 44bd2981b2be1a40253945b7e02b297a297b140b Mon Sep 17 00:00:00 2001 From: James Lamb Date: Thu, 10 Aug 2023 08:40:59 -0500 Subject: [PATCH] [R] remove default values in internal utility functions (#9457) --- R-package/R/utils.R | 8 ++++---- R-package/R/xgb.cv.R | 14 ++++++++++++-- R-package/R/xgb.train.R | 17 ++++++++++++++--- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/R-package/R/utils.R b/R-package/R/utils.R index a822113a7..458b119f6 100644 --- a/R-package/R/utils.R +++ b/R-package/R/utils.R @@ -140,7 +140,7 @@ check.custom.eval <- function(env = parent.frame()) { # Update a booster handle for an iteration with dtrain data -xgb.iter.update <- function(booster_handle, dtrain, iter, obj = NULL) { +xgb.iter.update <- function(booster_handle, dtrain, iter, obj) { if (!identical(class(booster_handle), "xgb.Booster.handle")) { stop("booster_handle must be of xgb.Booster.handle class") } @@ -163,7 +163,7 @@ xgb.iter.update <- function(booster_handle, dtrain, iter, obj = NULL) { # Evaluate one iteration. # Returns a named vector of evaluation metrics # with the names in a 'datasetname-metricname' format. -xgb.iter.eval <- function(booster_handle, watchlist, iter, feval = NULL) { +xgb.iter.eval <- function(booster_handle, watchlist, iter, feval) { if (!identical(class(booster_handle), "xgb.Booster.handle")) stop("class of booster_handle must be xgb.Booster.handle") @@ -234,7 +234,7 @@ generate.cv.folds <- function(nfold, nrows, stratified, label, params) { y <- factor(y) } } - folds <- xgb.createFolds(y, nfold) + folds <- xgb.createFolds(y = y, k = nfold) } else { # make simple non-stratified folds kstep <- length(rnd_idx) %/% nfold @@ -251,7 +251,7 @@ generate.cv.folds <- function(nfold, nrows, stratified, label, params) { # Creates CV folds stratified by the values of y. # It was borrowed from caret::createFolds and simplified # by always returning an unnamed list of fold indices. -xgb.createFolds <- function(y, k = 10) { +xgb.createFolds <- function(y, k) { if (is.numeric(y)) { ## Group the numeric data based on their magnitudes ## and sample within those groups. diff --git a/R-package/R/xgb.cv.R b/R-package/R/xgb.cv.R index 27730cbc3..788638921 100644 --- a/R-package/R/xgb.cv.R +++ b/R-package/R/xgb.cv.R @@ -223,8 +223,18 @@ xgb.cv <- function(params = list(), data, nrounds, nfold, label = NULL, missing for (f in cb$pre_iter) f() msg <- lapply(bst_folds, function(fd) { - xgb.iter.update(fd$bst, fd$dtrain, iteration - 1, obj) - xgb.iter.eval(fd$bst, fd$watchlist, iteration - 1, feval) + xgb.iter.update( + booster_handle = fd$bst, + dtrain = fd$dtrain, + iter = iteration - 1, + obj = obj + ) + xgb.iter.eval( + booster_handle = fd$bst, + watchlist = fd$watchlist, + iter = iteration - 1, + feval = feval + ) }) msg <- simplify2array(msg) bst_evaluation <- rowMeans(msg) diff --git a/R-package/R/xgb.train.R b/R-package/R/xgb.train.R index 5a7d2eb5e..729475945 100644 --- a/R-package/R/xgb.train.R +++ b/R-package/R/xgb.train.R @@ -390,10 +390,21 @@ xgb.train <- function(params = list(), data, nrounds, watchlist = list(), for (f in cb$pre_iter) f() - xgb.iter.update(bst$handle, dtrain, iteration - 1, obj) + xgb.iter.update( + booster_handle = bst$handle, + dtrain = dtrain, + iter = iteration - 1, + obj = obj + ) - if (length(watchlist) > 0) - bst_evaluation <- xgb.iter.eval(bst$handle, watchlist, iteration - 1, feval) # nolint: object_usage_linter + if (length(watchlist) > 0) { + bst_evaluation <- xgb.iter.eval( # nolint: object_usage_linter + booster_handle = bst$handle, + watchlist = watchlist, + iter = iteration - 1, + feval = feval + ) + } xgb.attr(bst$handle, 'niter') <- iteration - 1