From d1cc4e2fc4f61297c9378c1d25420ac69fe229ac Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Tue, 14 May 2024 23:02:33 -0400 Subject: [PATCH] Use global option for verbosity in R package and add tests. --- .../R/mlpack/tests/testthat/test-R_binding.R | 22 +++++++++++++++++++ src/mlpack/bindings/R/print_input_param.hpp | 15 ++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/mlpack/bindings/R/mlpack/tests/testthat/test-R_binding.R b/src/mlpack/bindings/R/mlpack/tests/testthat/test-R_binding.R index a921fd4d66..726f053706 100644 --- a/src/mlpack/bindings/R/mlpack/tests/testthat/test-R_binding.R +++ b/src/mlpack/bindings/R/mlpack/tests/testthat/test-R_binding.R @@ -335,3 +335,25 @@ test_that("TestReallyNotVerbose", { build_model=TRUE, verbose=FALSE)) }) + +# Make sure that the mlpack verbose global option does anything at all. +test_that("TestGlobalVerbose", { + options(mlpack.verbose = TRUE) + expect_output(test_r_binding(4.0, 12, "hello", + build_model=TRUE)) +}) + +# Test that we get no output when the global verbose option is set to false. +test_that("TestGlobalNotVerbose", { + options(mlpack.verbose = FALSE) + expect_silent(test_r_binding(4.0, 12, "hello", + build_model=TRUE)) +}) + +# Test that we can override the global verbose option. +test_that("TestGlobalVerboseOverride", { + options(mlpack.verbose = TRUE) + expect_silent(test_r_binding(4.0, 12, "hello", + build_model=TRUE, + verbose=FALSE)) +}) diff --git a/src/mlpack/bindings/R/print_input_param.hpp b/src/mlpack/bindings/R/print_input_param.hpp index 024676e570..4af58ca80c 100644 --- a/src/mlpack/bindings/R/print_input_param.hpp +++ b/src/mlpack/bindings/R/print_input_param.hpp @@ -30,9 +30,22 @@ void PrintInputParam(util::ParamData& d, { MLPACK_COUT_STREAM << d.name; if (std::is_same::value) - MLPACK_COUT_STREAM << "=FALSE"; + { + if (d.name == "verbose") + { + // Make sure that we use the global verbose option for the mlpack package + // as the default. + MLPACK_COUT_STREAM << "=getOption(\"mlpack.verbose\", FALSE)"; + } + else + { + MLPACK_COUT_STREAM << "=FALSE"; + } + } else if (!d.required) + { MLPACK_COUT_STREAM << "=NA"; + } } } // namespace r