with_msgr: Convert messages, warnings and stops into msgr messages

Description Usage Arguments Details Value Examples

View source: R/try-catch.R

Description

This function allows you to execute an expression and all messages, warnings and stops are converted to info(), warn() and error() respectively.

Usage

1
2
3
4
5
6
7
8
9
with_msgr(
  expr,
  level = 1,
  warn_level = level,
  error_level = 1,
  msg_level = getOption("msgr.level"),
  msg_types = getOption("msgr.types"),
  log_path = getOption("msgr.log_path")
)

Arguments

expr

(expression) The expression to execute.

level

(integer, optional) The level of messages, from 1 to 10. Default: 1.

warn_level

(integer, optional) The level of warnings, from 1 to 10. Default: 1.

error_level

(integer, optional) The level of errors, from 1 to 10. Default: 1.

msg_level

(integer, optional) The maximum level of messages to output. Default: set in the option "msgr.level".

msg_types

(character, optional) The type to write or display. Must either NULL or one or more from "INFO", "WARNING" or "ERROR". Default: set in the option "msgr.types".

log_path

(string, optional) The file path to the text log file. If set to "", then no logs are written. Default: set in the option "msgr.log_path".

Details

This function can also be used to change the level of message, so if a function raises a message with level 1, it can be downgraded to level 2+. Similarly it can also be used to modify the msg_level. For example, if the current msg_level is set to 1, and a function raises a message with level 3, the message will not be displayed. If you want it displayed for this function but not everywhere else, you can use with_msgr() and set the msg_level to 3 for that call alone.

Value

Whatever the expression returns

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
## Not run: 

test_msgr <- function(x) {
  if (x == "msg") message("This is a message")
  if (x == "wrn") warning("This is a warning")
  if (x == "err") stop("This is an error")
}

with_msgr(test_msgr("msg"))
with_msgr(test_msgr("wrn"))
with_msgr(test_msgr("err"))

with_msgr(test_msgr("msg"), level = 2)
with_msgr(test_msgr("wrn"), level = 2)
with_msgr(test_msgr("err"), level = 2)

test_lvl <- function(x) {
  if (x == "msg") info("This is a message", level = 3)
  if (x == "wrn") warn("This is a warning", level = 3)
  if (x == "err") error("This is an error", level = 3)
}

with_msgr(test_lvl("msg"), msg_level = 3)
with_msgr(test_lvl("wrn"), msg_level = 3)
with_msgr(test_lvl("err"), msg_level = 3)


## End(Not run)

ChadGoymer/msgr documentation built on April 10, 2021, 10:31 a.m.