validate_and_run: Validate and run

Description Usage Details Value Examples

Description

Validate input and run calling method. Template to structure and test larger methods.

Usage

1

Details

First validate is called. If any errors are added, the value of invalid_result is returned, else the value of result. Loads the following methods into the environment of the calling method:

Value

If any errors are added, the value of invalid_result is returned, else the value of result.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
two_times_two <- function(two, also_two) {
  result <- function() helper()

  helper <- function() two * also_two

  validate <- function() {
    if(two != 2) add_error('two', 'should be 2')
    if(also_two != 2) add_error('also_two', 'should also be two')
  }

  invalid_result <- function() {
    list(result=NA, errors=errors())
  }

  ShadowCAT:::validate_and_run()
}

# No errors
two_times_two(2,2)

# Error
two_times_two(3,2)

Karel-Kroeze/ShadowCAT documentation built on May 7, 2019, 12:28 p.m.