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

Enhancement/quiet-messaging #77

Merged
merged 11 commits into from
Feb 13, 2024
Merged
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export(generate_params)
export(hector_matrix)
export(is.criterion)
export(iterate_model)
export(matilda_message)
export(matilda_warning)
export(metric_calc)
export(metric_calc_1run)
export(multi_criteria_weighting)
Expand Down
47 changes: 47 additions & 0 deletions R/hush.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Set an option to control whether messages and warning are suppressed.
# Defaults to verbose messaging.
options("matilda.verbose" = FALSE)

# Quiet messages

#' Suppressing messages in Matilda functions
#'
#' @param ... Message to print if verbosity option is set to TRUE.
#'
#' @export
#'
matilda_message <- function(...) {

# Get the current verbose option setting
message.option <- getOption("matilda.verbose")

# Check if the verbose option is set to FALSE
if (message.option == FALSE) {
# If FALSE, exit function without displaying message
return()
}
# If verbose option is TRUE, display message
message(...)
}

# Quiet warnings

#' Suppressing warnings in Matilda functions
#'
#' @param ... Warning to print if verbosity option is set to TRUE.
#'
#' @export
#'
matilda_warning <- function(...) {

# Get the current verbose option setting
warning.option <- getOption("matilda.verbose")

# Check if the verbose option if set to FALSE
if (warning.option == FALSE) {
# If FALSE, exit the function without displaying message
return()
}
# If verbose option is TRUE, display message
warning(...)
}
1 change: 1 addition & 0 deletions R/iterate_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ metric_calc_1run <- function(x, metric) {
#' @export
#'
#' @examples
#' options("matilda.verbose" = TRUE)
#' # Load scenario file and initiate a new Hector core
#' ssp245 <- system.file("input/hector_ssp245.ini", package = "hector")
#' core <- newcore(ssp245)
Expand Down
4 changes: 2 additions & 2 deletions R/set_params.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
set_params <- function(core, param_values) {
# If parameters values are not numeric - stop and send error
if (length(param_values) == 0) {
warning("no parameters")
matilda_warning("no parameters")
return()
}

Expand All @@ -43,7 +43,7 @@ set_params <- function(core, param_values) {
var_units <- getunits(var)

# send a message for what each var is being set to
message("setting ", var, " to ", param_values[i])
matilda_message("setting ", var, " to ", param_values[i])

# set variables to be passed to
setvar(core, NA, var = var, values = param_values[i], unit = var_units)
Expand Down
1 change: 1 addition & 0 deletions man/iterate_model.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions man/matilda_message.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions man/matilda_warning.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tests/testthat/test-set_params.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ y <- c("BETA" = 1, "Q10_RH" = 2)
# core
core <- newcore(system.file("input/hector_ssp245.ini", package = "hector"))

# set verbose option to TRUE
options(matilda.verbose = TRUE)

# Checking message printed - may not be necessary
test_that("function returns message", {
expect_message(set_params(
Expand Down
6 changes: 5 additions & 1 deletion vignettes/matilda-vignette.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ First, load the `matilda` package
library(matilda)
```

```{r, echo=FALSE}
options("matilda.verbose" = FALSE)
```

Next, we will initialize a "core" for a new Hector instance.
More information about establishing a new core for running Hector can be found in the tutorial for using the [Hector R interface](https://jgcri.github.io/hector/articles/intro-to-hector.html).

Expand Down Expand Up @@ -80,7 +84,7 @@ The `iterate_model()` runs Hector multiple times, setting new parameter values w

Running `iterate_model()` requires a Hector core and a data frame of parameter values.

```{r, message=FALSE}
```{r}
# Run Hector repeatedly over all parameter values
results <- iterate_model(
core = c_ssp245,
Expand Down
Loading