raincheck: Generate external function for argument manipulation

Description Usage Arguments Details Value Note Author(s) References Examples

View source: R/raincheck.R

Description

The function raincheck creates a function that will manipulate the environment where it is called. The scold function writes error messages that appear to come from that same environment. Both functions make it easier to separate code that reformats arguments from the main code of a function.

Usage

1
2
raincheck(expr)
scold(text, action = c("warning", "stop", "message"),...)

Arguments

expr

R code that will operate on the environment of the function that calls the function returned by raincheck.

text

An error message to display to the user.

action

The type of error message.

...

Additional arguments to pass to the condition generated in scold.

Details

Fill in after the package evolves a little.

Value

raincheck returns a function that manipulates the environment where it is called. See the examples below.

Note

Some code in the scold function is based on/borrowed from Wickham's (2013) Advanced R programming, see http://adv-r.had.co.nz/Exceptions-Debugging.html#condition-handling.

Author(s)

Gray Calhoun gcalhoun@iastate.edu

References

Wickham, Hadley (2013) Advanced R Programming. Chapman and Hall. http://adv-r.had.co.nz/

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
f1 <- function(x) {
  FormatArguments_f1() # Yikes; *only* side effects
  ## Important, logical stuff goes here
  x[10,10]
} 

FormatArguments_f1 <- raincheck({
  if (!is.matrix(x)) {
    scold("'x' really should have been a matrix.")
    x <- as.matrix(x)
  }
  if (any(x == 30)) {
    scold("Cool, I love the number thirty.\n", "message")
  }
  if (nrow(x) < 10 || ncol(x) < 10) {
    scold("'x' must have at least 10 rows and 10 columns.", "stop")
  }
})

f1(matrix(30, 11, 11))
f1(matrix(31, 11, 11))

## Not run: f1(30)

grayclhn/raincheck documentation built on May 17, 2019, 8:34 a.m.