if (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()')
}
# normal run (no warnings or errors)
out <- fun()
out
# these are all the same
manageWarnings(out <- fun())
out <- manageWarnings(fun())
out <- fun() |> manageWarnings()
# errors treated normally
fun(error=TRUE)
fun(error=TRUE) |> manageWarnings()
# all warnings/returns treated normally by default
ret1 <- fun(warn1=TRUE)
ret2 <- fun(warn1=TRUE) |> manageWarnings()
identical(ret1, ret2)
# all warnings converted to errors (similar to options(warn=2), but local)
fun(warn1=TRUE) |> manageWarnings(warning2error=TRUE)
fun(warn2=TRUE) |> manageWarnings(warning2error=TRUE)
# Specific warnings treated as errors (others stay as warnings)
# Here, treat first warning message as error but not the second or third
ret <- fun(warn1=TRUE) # warning
ret <- fun(warn1=TRUE) |> manageWarnings("Message one") # now error
ret <- fun(warn2=TRUE) |> manageWarnings("Message one") # still a warning
# multiple warnings raised but not converted as they do not match criteria
fun(warn2=TRUE, warn3=TRUE)
fun(warn2=TRUE, warn3=TRUE) |> manageWarnings("Message one")
# Explicitly convert multiple warning messages, allowing others through.
# This is generally the best use of the function's specificity
fun(warn1=TRUE, warn2=TRUE)
fun(warn1=TRUE) |> # error given either message
manageWarnings(c("Message one", "Message two"))
fun(warn2=TRUE) |>
manageWarnings(c("Message one", "Message two"))
# last warning gets through (left as valid warning)
ret <- fun(warn3=TRUE) |>
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)
# partial match, therefore suppressed
fun(warn_trailing=TRUE) |>
manageWarnings(suppress="Message with lots of random trailings: ")
# multiple suppress strings
fun(warn_trailing=TRUE) |>
manageWarnings(suppress=c("Message with lots of random trailings: ",
"Suppress this too"))
# 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
fun <- function(warn1=FALSE, warn2=FALSE, warn3=FALSE, error=FALSE){
message('This function is rather chatty')
cat("It even prints in different output forms!\n")
if(warn1) warning('Message one')
if(warn2) warning('Message two')
if(warn3) warning('Message three')
if(error) stop('terminate function call')
return('Returned from fun()')
}
# normal run (no warnings or errors, but messages)
out <- fun()
out <- quiet(fun()) # using "indoor voice"
# suppress all print messages and warnings (not recommended)
fun(warn2=TRUE) |> quiet()
fun(warn2=TRUE) |> quiet() |> suppressWarnings()
# convert all warning to errors, and keep suppressing messages via quiet()
fun(warn2=TRUE) |> quiet() |> manageWarnings(warning2error=TRUE)
# define tolerable warning messages (only warn1 deemed ignorable)
ret <- fun(warn1=TRUE) |> quiet() |>
manageWarnings(suppress = 'Message one')
# all other warnings raised to an error except ignorable ones
fun(warn1=TRUE, warn2=TRUE) |> quiet() |>
manageWarnings(warning2error=TRUE, suppress = 'Message one')
# only warn2 raised to an error explicitly (warn3 remains as warning)
ret <- fun(warn1=TRUE, warn3=TRUE) |> quiet() |>
manageWarnings(warning2error = 'Message two',
suppress = 'Message one')
fun(warn1=TRUE, warn2 = TRUE, warn3=TRUE) |> quiet() |>
manageWarnings(warning2error = 'Message two',
suppress = 'Message one')
###########################
# Practical example, converting warning into error for model that
# failed to converged normally
library(lavaan)
## The industrialization and Political Democracy Example
## Bollen (1989), page 332
model <- '
# latent variable definitions
ind60 =~ x1 + x2 + x3
dem60 =~ y1 + a*y2 + b*y3 + c*y4
dem65 =~ y5 + a*y6 + b*y7 + c*y8
# regressions
dem60 ~ ind60
dem65 ~ ind60 + dem60
# residual correlations
y1 ~~ y5
y2 ~~ y4 + y6
y3 ~~ y7
y4 ~~ y8
y6 ~~ y8
'
# throws a warning
fit <- sem(model, data = PoliticalDemocracy, control=list(iter.max=60))
# for a simulation study, often better to treat this as an error
fit <- sem(model, data = PoliticalDemocracy, control=list(iter.max=60)) |>
manageWarnings(warning2error = "the optimizer warns that a solution has NOT been found!")
}
Run the code above in your browser using DataLab