diff --git a/NAMESPACE b/NAMESPACE index 5f96752..f3ec2b5 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/hush.R b/R/hush.R new file mode 100644 index 0000000..4cae2c4 --- /dev/null +++ b/R/hush.R @@ -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(...) +} diff --git a/R/iterate_model.R b/R/iterate_model.R index 8bb56bb..2fa94c9 100644 --- a/R/iterate_model.R +++ b/R/iterate_model.R @@ -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) diff --git a/R/set_params.R b/R/set_params.R index 6b44a4e..916e2aa 100644 --- a/R/set_params.R +++ b/R/set_params.R @@ -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() } @@ -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) diff --git a/man/iterate_model.Rd b/man/iterate_model.Rd index 2706cc1..47d1849 100644 --- a/man/iterate_model.Rd +++ b/man/iterate_model.Rd @@ -31,6 +31,7 @@ the \code{\link{generate_params}} function. Output of model iterations will be filtered according to metric information. } \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) diff --git a/man/matilda_message.Rd b/man/matilda_message.Rd new file mode 100644 index 0000000..eed7cb8 --- /dev/null +++ b/man/matilda_message.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/hush.R +\name{matilda_message} +\alias{matilda_message} +\title{Suppressing messages in Matilda functions} +\usage{ +matilda_message(...) +} +\arguments{ +\item{...}{Message to print if verbosity option is set to TRUE.} +} +\description{ +Suppressing messages in Matilda functions +} diff --git a/man/matilda_warning.Rd b/man/matilda_warning.Rd new file mode 100644 index 0000000..baac412 --- /dev/null +++ b/man/matilda_warning.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/hush.R +\name{matilda_warning} +\alias{matilda_warning} +\title{Suppressing warnings in Matilda functions} +\usage{ +matilda_warning(...) +} +\arguments{ +\item{...}{Warning to print if verbosity option is set to TRUE.} +} +\description{ +Suppressing warnings in Matilda functions +} diff --git a/tests/testthat/test-set_params.R b/tests/testthat/test-set_params.R index f951572..9fd3a8a 100644 --- a/tests/testthat/test-set_params.R +++ b/tests/testthat/test-set_params.R @@ -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( diff --git a/vignettes/matilda-vignette.Rmd b/vignettes/matilda-vignette.Rmd index f1c08d9..450fd65 100644 --- a/vignettes/matilda-vignette.Rmd +++ b/vignettes/matilda-vignette.Rmd @@ -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). @@ -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,