Giter VIP home page Giter VIP logo

conditionr's Introduction

conditionr

Custom R conditions

Installation

require("devtools")
devtools::install_github("Rappster/conditionr")
require("conditionr")

Convenience functions

All of the following functions are mere convenience wrappers around conditionr::signalCondition() (see below).

Messages

newMessage()
newMessage("CustomMessage", c("Header", Hello = "World!", "Unnamed element"))
newMessage("CustomMessage", c("Some information", Hello = "World!", "Unnamed element"),
  header = FALSE)
newMessage("CustomMessage", "Some information", time = FALSE)
newMessage("CustomMessage", "Some information", pid = FALSE)
newMessage("CustomMessage", "Some information", include_condition = FALSE)

## Typical use case inside of a function
foo <- function(x) {
    withCallingHandlers({
      out <- log(x)
      newMessage("StatusIsOk", "Everything worked just fine")
      out},
      warning = function(cond) {
        newWarning("StatusIsWarning",
          c("We have a warning", "Actual warning message" = conditionMessage(cond))
        )
        invokeRestart("muffleWarning")
      },
      error = function(cond) {
        newError("StatusIsError",
          c("We have an error", "Actual error message" = conditionMessage(cond))
        )
      }
    )
}
(x <- foo(1))
(x <- foo(-1))
(x <- foo("a"))

Warnings

newWarning()
newWarning("CustomWarning", c("Header", Hello = "World!", "Unnamed element"))
newWarning("CustomWarning", c("Some information", Hello = "World!", "Unnamed element"),
  header = FALSE)
newWarning("CustomWarning", "Some information", time = FALSE)
newWarning("CustomWarning", "Some information", pid = FALSE)
newWarning("CustomWarning", "Some information", include_condition = FALSE)

Errors

newError()
newError("CustomError", c("Header", Hello = "World!", "Unnamed element"))
newError("CustomError", c("Some information", Hello = "World!", "Unnamed element"),
  header = FALSE)
newError("CustomError", "Some information", time = FALSE)
newError("CustomError", "Some information", pid = FALSE)
newError("CustomError", "Some information", include_condition = FALSE)

Details on conditionr::signalCondition()

Messages

# 1) Emtpy messag -------------------------------------------------------

## Signaled right away //
## Note that an actuall message is issued in the console
res <- signalCondition()
class(res)

## Not signaled right away, only condition object is returned //
res <- signalCondition(signal = FALSE)
res
class(res)
## Explicit issuing:
message(res)

## Custom condition class(es) //
res <- signalCondition(condition = "CustomMessage")
class(res)
res <- signalCondition(condition = c("CustomMessage1", "CustomMessage2"))
class(res)


# 2) Custom message -----------------------------------------------

## Single-line:
res <- signalCondition(
  condition = "CustomMessage",
  msg = "Hello world!",
  signal = FALSE
)
res
message(res)
## --> note that condition classis only displayed for the *unsignaled*
## condition. That's why the condition itself is also included in the prefix
## by default (can be suppressed via `include_condition`)

## Suppress time:
res <- signalCondition(
  condition = "CustomMessage",
  msg = "Hello world!",
  signal = FALSE,
  time = FALSE
)
res
message(res)

## Suppress PID:
res <- signalCondition(
  condition = "CustomMessage",
  msg = "Hello world!",
  signal = FALSE,
  pid = FALSE
)
res
message(res)

## Suppress condition in prefix:
res <- signalCondition(
  condition = "CustomMessage",
  msg = "Hello world!",
  signal = FALSE,
  include_condition = FALSE
)
res
message(res)

## Suppress time, PID and condition:
res <- signalCondition(
  condition = "CustomMessage",
  msg = "Hello world!",
  signal = FALSE,
  time = FALSE,
  pid = FALSE,
  include_condition = FALSE
)
res
message(res)

## Multi-line:
res <- signalCondition(
  condition = "CustomMessage",
  msg = c(Hello = "world!"),
  signal = FALSE
)
res
message(res)

## With header:
res <- signalCondition(
  condition = "CustomMessage",
  msg = c("Header", "Name with whitespace" = "test", Hello = "World!"),
  signal = FALSE
)
res
message(res)

## Without header:
res <- signalCondition(
  condition = "CustomMessage",
  msg = c("Name with whitespace" = "test", "Unnamed element", Hello = "World!"),
  signal = FALSE
)
res
message(res)
## --> note that named elements are never used as headers

res <- signalCondition(
  condition = "CustomMessage",
  msg = c("I'm not a header", "Unnamed element",
    "Name with whitespace" = "test", Hello = "World!"),
  signal = FALSE,
  header = FALSE
)
res
message(res)
## --> note that if `msg` contains an unnamed element at the first position,
## it will be treated as the header unless `header = FALSE`

## Include namespace //
## You can use this argument in order to denote the package/namespace
## that contains the function/method/closure that produces the condition
res <- signalCondition(ns = "my.package", signal = FALSE)
res

## Illustration of the typical use within a function //
## Note that the name of the function is automatically added:
foo <- function(x = TRUE) {
  if (x == TRUE) {
    return("Hello World!")
  } else {
    signalCondition(
      condition = "AppropriateCondition",
      msg = c(
        "This is the header",
        Details = "illustration of a custom condition",
        "Some more information"
      ),
      ns = "my.package",
      type = "message"
    )
    return("Hello World!")
  }
}
foo()
foo(x = FALSE)

Warnings

# 1) Empty warning --------------------------------------------------------

## Signaled right away //
## Note that an actuall warning is issued in the console
res <- signalCondition(type = "warning")
class(res)

## Not signaled, only condition object is returned //
res <- signalCondition(type = "warning", signal = FALSE)
res
class(res)
## Explicit issuing:
warning(res)
## --> we gave no warning message, that's why nothing is issued

## Custom condition class(es) //
res <- signalCondition(
  condition = "CustomWarning",
  type = "warning"
)
class(res)

res <- signalCondition(
  condition = c("CustomWarning1", "CustomWarning2"),
  type = "warning"
)
class(res)

# 2) Custom warning message -----------------------------------------------

## Single-line:
res <- signalCondition(
  condition = "CustomWarning",
  msg = "Hello world!",
  type = "warning",
  signal = FALSE
)
res
warning(res)
## --> note that condition classis only displayed for the *unsignaled*
## condition. That's why the condition itself is also included in the prefix
## by default (can be suppressed via `include_condition`)

## Suppress time:
res <- signalCondition(
  condition = "CustomWarning",
  msg = "Hello world!",
  type = "warning",
  signal = FALSE,
  time = FALSE
)
res
warning(res)

## Suppress PID:
res <- signalCondition(
  condition = "CustomWarning",
  msg = "Hello world!",
  type = "warning",
  signal = FALSE,
  pid = FALSE
)
res
warning(res)

## Suppress condition in prefix:
res <- signalCondition(
  condition = "CustomWarning",
  msg = "Hello world!",
  type = "warning",
  signal = FALSE,
  include_condition = FALSE
)
res
warning(res)

## Suppress time, PID and condition:
res <- signalCondition(
  condition = "CustomWarning",
  msg = "Hello world!",
  type = "warning",
  signal = FALSE,
  time = FALSE,
  pid = FALSE,
  include_condition = FALSE
)
res
warning(res)

## Multi-line:
res <- signalCondition(
  condition = "CustomWarning",
  msg = c(Hello = "world!"),
  type = "warning",
  signal = FALSE
)
res
warning(res)

## With header:
res <- signalCondition(
  condition = "CustomWarning",
  msg = c("Header", "Name with whitespace" = "test", Hello = "World!"),
  type = "warning",
  signal = FALSE
)
res
warning(res)

## Without header:
res <- signalCondition(
  condition = "CustomWarning",
  msg = c("Name with whitespace" = "test", "Unnamed element", Hello = "World!"),
  type = "warning",
  signal = FALSE
)
res
warning(res)
## --> note that named elements are never used as headers

res <- signalCondition(
  condition = "CustomWarning",
  msg = c("I'm not a header", "Unnamed element",
    "Name with whitespace" = "test", Hello = "World!"),
  type = "warning",
  signal = FALSE,
  header = FALSE
)
res
warning(res)
## --> note that if `msg` contains an unnamed element at the first position,
## it will be treated as the header unless `header = FALSE`

## Include namespace //
## You can use this argument in order to denote the package/namespace
## that contains the function/method/closure that produces the condition
res <- signalCondition(ns = "my.package", type = "warning", signal = FALSE)
warning(res)

## Illustration of the typical use within a function //
## Note that the name of the function is automatically added:
foo <- function(x = TRUE) {
  if (x == TRUE) {
    return("Hello World!")
  } else {
    signalCondition(
      condition = "AppropriateCondition",
      msg = c(
        "This is the header",
        Details = "illustration of a custom condition",
        "Some more information"
      ),
      ns = "my.package",
      type = "warning"
    )
    return("Hello World!")
  }
}
foo()
foo(x = FALSE)

Errors

# 1) Empty error ----------------------------------------------------------

## Signaled right away //
## Note that an actuall error is issued in the console
signalCondition(type = "error")

## Not signaled, only condition object is returned //
res <- signalCondition(type = "error", signal = FALSE)
res

## Explicit issuing:
stop(res)

## Custom condition class(es) //
res <- signalCondition(
  condition = "CustomError",
  type = "error",
  signal = FALSE
)
class(res)
res <- signalCondition(
  condition = c("CustomError1", "CustomError2"),
  type = "error",
  signal = FALSE
)
class(res)


# 2) Custom error message -------------------------------------------------

## Single-line:
res <- signalCondition(
  condition = "CustomError",
  msg = "Hello world!",
  type = "error",
  signal = FALSE
)
res
stop(res)
## --> note that condition classis only displayed for the *unsignaled*
## condition. That's why the condition itself is also included in the prefix
## by default (can be suppressed via `include_condition`)

## Suppress time:
res <- signalCondition(
  condition = "CustomError",
  msg = "Hello world!",
  type = "error",
  signal = FALSE,
  time = FALSE
)
res
stop(res)

## Suppress PID:
res <- signalCondition(
  condition = "CustomError",
  msg = "Hello world!",
  type = "error",
  signal = FALSE,
  pid = FALSE
)
res
stop(res)

## Suppress condition in prefix:
res <- signalCondition(
  condition = "CustomError",
  msg = "Hello world!",
  type = "error",
  signal = FALSE,
  include_condition = FALSE
)
res
stop(res)

## Suppress time, PID and condition:
res <- signalCondition(
  condition = "CustomError",
  msg = "Hello world!",
  type = "error",
  signal = FALSE,
  time = FALSE,
  pid = FALSE,
  include_condition = FALSE
)
res
stop(res)

## Multi-line:
res <- signalCondition(
  condition = "CustomError",
  msg = c(Hello = "world!"),
  type = "error",
  signal = FALSE
)
res
stop(res)

## With header:
res <- signalCondition(
  condition = "CustomError",
  msg = c("Header", "Name with whitespace" = "test", Hello = "World!"),
  type = "error",
  signal = FALSE
)
res
stop(res)

## Without header:
res <- signalCondition(
  condition = "CustomError",
  msg = c("Name with whitespace" = "test", "Unnamed element", Hello = "World!"),
  type = "error",
  signal = FALSE
)
res
stop(res)
## --> note that named elements are never used as headers

res <- signalCondition(
  condition = "CustomError",
  msg = c("I'm not a header", "Unnamed element",
    "Name with whitespace" = "test", Hello = "World!"),
  type = "error",
  signal = FALSE,
  header = FALSE
)
res
stop(res)
## --> note that if `msg` contains an unnamed element at the first position,
## it will be treated as the header unless `header = FALSE`

## Include namespace //
## You can use this argument in order to denote the package/namespace
## that contains the function/method/closure that produces the condition
res <- signalCondition(ns = "my.package", type = "error", signal = FALSE)
stop(res)

## Illustration of the typical use within a function //
## Note that the name of the function is automatically added:
foo <- function(x = TRUE) {
  if (x == TRUE) {
    return("Hello World!")
  } else {
    signalCondition(
      condition = "AppropriateCondition",
      msg = c(
        "This is the header",
        Details = "illustration of a custom condition",
        "Some more information"
      ),
      ns = "my.package",
      type = "error"
    )
    return("Hello World!")
  }
}
foo()
foo(x = FALSE)

conditionr's People

Contributors

jankowtf avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.