MyNixOS website logo
Description

Conveniently Issue Messages, Warnings, and Errors.

Provides tools for creating and issuing nicely-formatted text within R diagnostic messages and those messages given during warnings and errors. The formatting of the messages can be customized using templating features. Issues with singular and plural forms can be handled through specialized syntax.

Travis buildstatus Coveragestatus CRANstatus

The messaging Package

The goal of messaging is to provides a toolset for creating and issuing nicely-formatted text within R diagnostic messages and those messages given during warnings and errors. The formatting of the messages can be customized using templating features. Issues with singular and plural forms can be handled through specialized syntax.

Example for Emitting Messages

For for the purpose of demonstrating how messaging can help you format and issue messages, we can create a function (yield_a_message()) that uses the emit_message() function for the sole purpose of displaying a message.

library(messaging)

yield_a_message <- function() {
  
  # This function's only aim is to produce
  # a message; throughout the function body, we
  # can generate objects that can be assembled
  # into a customizable message with `emit_message()`
  
  # Create some strings can serve as additional
  # info for the message 
  important_message_parts <- 
    c("* message info part 1",
      "* message info part 2",
      "* message info part 3")
  
  # Get a numeric value to use in the message
  n_info_lines <- length(important_message_parts)
  
  # Generate and emit a formatted message
  emit_message(
    "There (is/are) {number} thing(s) to note", # on line 1; singular/plural syntax
    important_message_parts, # 3 additional lines (all lines are separated with `/n`)
    number = n_info_lines, # a named argument; `n_info_lines` inserted into {number}
    .format = "{.f_name} info: {text}") # an optional template
}

Within the main function body, there’s the creation of character (important_message_parts) and numeric (number_of_info_lines) values that will help us compose our message. This combination of function-scope objects will work alongside templated expression strings in the emit_message() function. Multiple expression strings can be provided to emit_message() to serve as components of the message (newlines will separate each of the expressions provided).

Using curly braces, we can enable text interpolation (e.g., The number is {number} and this is considered {level}) where named arguments (e.g., number = 3, level = "high", etc.) provide the values for the message components.

If there is interpolation of numerical values, there is also the option to provide singular and plural forms for words associated with the the values. Simply enclose the singular and plural alternatives in parentheses and the function will finalize the statement based on the associated numerical values. Some syntax examples are: (is/are), analys(is/es), result(s).

Upon calling yield_a_message() we see that a message does appear and that the text on the first line has been processed such that it’s grammatically correct.

# Call the `yield_a_message()` function
yield_a_message()
#> `yield_a_message()` info: There are 3 things to note
#> * message info part 1
#> * message info part 2
#> * message info part 3

We can also emit warnings and errors by using the emit_warning() and emit_error() functions within those functions of our own design. Here’s a function that might provide a warning (if the sum of x and y is greater than 100):

might_warn_you <- function(x, y) {
  
  sum_xy <- x + y

  if (sum_xy > 100) {
    emit_warning(
      "This value ({sum_xy}) is greater than 100.",
      sum_xy = sum_xy)
  }
  
  return(sum_xy)
}
might_warn_you(40, 50)
#> [1] 90
might_warn_you(60, 50)
#> Warning: `might_warn_you()`: This value (110) is greater than 100.
#> [1] 110

We can exit a function early with a message using the emit_error() function:

will_stop_if_not_careful <- function(x, y) {
  
  sum_xy <- x + y

  if (sum_xy > 100) {
    emit_error(
      "This value ({sum_xy}) is greater than 100. That is bad.",
      sum_xy = sum_xy)
  }
  
  return(sum_xy)
}
will_stop_if_not_careful(60, 50)

Here is a reproduction of the error message:

Error: `will_stop_if_not_careful()`: This value (110) is greater than 100. That is bad.

Installation of the package

messaging is used in an R environment. If you don’t have an R installation, it can be obtained from the Comprehensive R Archive Network (CRAN).

You can install the development version of messaging from GitHub using the devtools package.

devtools::install_github("rich-iannone/messaging")

If you encounter a bug, have usage questions, or want to share ideas to make this package better, feel free to file an issue.

Code of Conduct

Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

License

MIT © Richard Iannone.

Metadata

Version

0.1.0

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows