Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[R-package] Use cat() instead of print() for metrics and callbacks #6171

Merged
merged 4 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .ci/lint_r_code.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ LINTERS_TO_USE <- list(
, "true_false" = lintr::T_and_F_symbol_linter()
, "undesirable_function" = lintr::undesirable_function_linter(
fun = c(
"cat" = "CRAN forbids the use of cat() in packages except in special cases. Use message() or warning()."
, "cbind" = paste0(
"cbind" = paste0(
"cbind is an unsafe way to build up a data frame. merge() or direct "
, "column assignment is preferred."
)
Expand Down
10 changes: 5 additions & 5 deletions R-package/R/callback.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ cb_print_evaluation <- function(period) {

# Check if message is existing
if (nchar(msg) > 0L) {
print(.merge_eval_string(env = env))
cat(.merge_eval_string(env = env), "\n")
}

}
Expand Down Expand Up @@ -208,9 +208,9 @@ cb_early_stop <- function(stopping_rounds, first_metric_only, verbose) {
msg <- paste0(
"Will train until there is no improvement in "
, stopping_rounds
, " rounds."
, " rounds.\n"
)
print(msg)
cat(msg)
}

# Internally treat everything as a maximization task
Expand Down Expand Up @@ -284,7 +284,7 @@ cb_early_stop <- function(stopping_rounds, first_metric_only, verbose) {
}

if (isTRUE(verbose)) {
print(paste0("Early stopping, best iteration is: ", best_msg[[i]]))
cat(paste0("Early stopping, best iteration is: ", best_msg[[i]], "\n"))
}

# Store best iteration and stop
Expand All @@ -302,7 +302,7 @@ cb_early_stop <- function(stopping_rounds, first_metric_only, verbose) {
}

if (isTRUE(verbose)) {
print(paste0("Did not meet early stopping, best iteration is: ", best_msg[[i]]))
cat(paste0("Did not meet early stopping, best iteration is: ", best_msg[[i]], "\n"))
}

# Store best iteration and stop
Expand Down
23 changes: 23 additions & 0 deletions R-package/tests/testthat/test_basic.R
Original file line number Diff line number Diff line change
Expand Up @@ -3805,3 +3805,26 @@ test_that("lightgbm() correctly sets objective when passing lgb.Dataset as input
)
expect_equal(model$params$objective, "regression")
})

test_that("Evaluation metrics aren't printed as a single-element vector", {
log_txt <- capture_output({
data(mtcars)
y <- mtcars$mpg
x <- as.matrix(mtcars[, -1L])
cv_result <- lgb.cv(
data = lgb.Dataset(x, label = y)
, params = list(
objective = "regression"
, metric = "l2"
, min_data_in_leaf = 5L
, max_depth = 3L
, num_threads = .LGB_MAX_THREADS
)
, nrounds = 2L
, nfold = 3L
, verbose = 1L
, eval_train_metric = TRUE
)
})
expect_false(grepl("[1] \"[1]", log_txt, fixed = TRUE))
})