diff --git a/R-package/R/utils.R b/R-package/R/utils.R index 9aa95e6cfcd9..3cdab4dcfd08 100644 --- a/R-package/R/utils.R +++ b/R-package/R/utils.R @@ -25,20 +25,19 @@ lgb.params2str <- function(params) { stop("params must be a list") } - # Split parameter names names(params) <- gsub("\\.", "_", names(params)) - + param_names <- names(params) ret <- list() # Perform key value join - for (key in names(params)) { + for (i in seq_along(params)) { # If a parameter has multiple values, join those values together with commas. # trimws() is necessary because format() will pad to make strings the same width val <- paste0( trimws( format( - x = params[[key]] + x = unname(params[[i]]) , scientific = FALSE ) ) @@ -47,7 +46,7 @@ lgb.params2str <- function(params) { if (nchar(val) <= 0L) next # Skip join # Join key value - pair <- paste0(c(key, val), collapse = "=") + pair <- paste0(c(param_names[[i]], val), collapse = "=") ret <- c(ret, pair) } diff --git a/R-package/tests/testthat/test_utils.R b/R-package/tests/testthat/test_utils.R index ed351af5a3f9..017dd071ebbb 100644 --- a/R-package/tests/testthat/test_utils.R +++ b/R-package/tests/testthat/test_utils.R @@ -26,6 +26,17 @@ test_that("lgb.params2str() works as expected for a key in params with multiple ) }) +test_that("lgb.params2str() passes through duplicated params", { + out_str <- lgb.params2str( + params = list( + objective = "regression" + , bagging_fraction = 0.8 + , bagging_fraction = 0.5 + ) + ) + expect_equal(out_str, "objective=regression bagging_fraction=0.8 bagging_fraction=0.5") +}) + context("lgb.check.eval") test_that("lgb.check.eval works as expected with no metric", {