Skip to content

Commit

Permalink
suppress should have allowed partial matching
Browse files Browse the repository at this point in the history
  • Loading branch information
philchalmers committed Aug 16, 2024
1 parent 286fcfe commit 84cffd6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
22 changes: 20 additions & 2 deletions R/manageWarnings.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,13 @@
#' @examples
#' \dontrun{
#'
#' fun <- function(warn1=FALSE, warn2=FALSE, warn3=FALSE, error=FALSE){
#' fun <- function(warn1=FALSE, warn2=FALSE, warn3=FALSE,
#' warn_trailing = FALSE, error=FALSE){
#' if(warn1) warning('Message one')
#' if(warn2) warning('Message two')
#' if(warn3) warning('Message three')
#' if(warn_trailing) warning(sprintf('Message with lots of random trailings: %s',
#' paste0(sample(letters, sample(1:20, 1)), collapse=',')))
#' if(error) stop('terminate function call')
#' return('Returned from fun()')
#' }
Expand Down Expand Up @@ -144,6 +147,20 @@
#' manageWarnings(c("Message one", "Message two"))
#' ret
#'
#' # suppress warnings that have only partial matching
#' fun(warn_trailing=TRUE)
#' fun(warn_trailing=TRUE)
#' fun(warn_trailing=TRUE)
#'
#' # does not match because string is not identical
#' fun(warn_trailing=TRUE) |>
#' manageWarnings(suppress="Message with lots of random trailings: ")
#'
#' # could also use .* to catch all remaining characters (finer regex control)
#' fun(warn_trailing=TRUE) |>
#' manageWarnings(suppress="Message with lots of random trailings: .*")
#'
#'
#' ###########
#' # Combine with quiet() and suppress argument to suppress innocuous messages
#'
Expand Down Expand Up @@ -223,7 +240,8 @@
#'
manageWarnings <- function(expr, warning2error = FALSE, suppress = NULL, ...){
stopit <- function(message, warning2error, suppress, ...){
if(message %in% suppress) return(TRUE)
if(!is.null(suppress))
if(grepl(suppress, message, ...)) return(TRUE)
if(is.null(warning2error)) stop(message, call.=FALSE)
sapply(warning2error, function(warn){
if(warn == "") return(invisible(NULL))
Expand Down
19 changes: 18 additions & 1 deletion man/manageWarnings.Rd

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

0 comments on commit 84cffd6

Please sign in to comment.