error_if: Function for checking arbitrary error conditions.

Description Usage Arguments Details Value See Also Examples

View source: R/error_if.R

Description

This function is designed for working with piping operator %>% from 'magrittr' package.

Usage

1
2
3
error_if(.data, expr, show = NULL)

.error_if(expr, show = NULL)

Arguments

.data

Dataset that will be checked.

expr

Expression that returns logical vector. TRUE is incorrect value (error), all other values are considered as valid cases.

show

Additional variables (such as 'id') that should be shown with report about errors.

Details

Supposed usage is: checked_data.frame %>% error_if(expression). It is possible to use this function with conditional operators check_if and check_subset. Both conditional operators will have the same effect: expression will be checked only for TRUE values in conditions.

Value

original data.frame .data with attribute with results of checking.

See Also

check_if, check_subset, %>%, move

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
library(dplyr)
data(ProductTestRaw)

# Age category and age should be consistent. 4 errors
ProductTestRaw  %>% error_if((s2a %in% 18:26) & !(s2b %in% 2))  %>% report
# No errors
ProductTestRaw  %>% error_if((s2a %in% 27:35) & !(s2b %in% 3))  %>% report

# Show 'id' and 'cell'
ProductTestRaw  %>% error_if((s2a %in% 18:26) & !(s2b %in% 2),show=c(id,cell))  %>% report

# Conditional checking. There  are no errors in cell 2.

ProductTestRaw  %>% check_if(cell==2)  %>% error_if((s2a %in% 18:26) & !(s2b %in% 2))  %>% report

# Usage in programming (e. g. in cycle 'for')
# Similar to Example 4 for sngl function.
checked_vars = c("a3","a22","b3","b23")

# there is one error in a22

for (each_var in checked_vars){
     cat("Question:",each_var,"\n")
     ProductTestRaw  %>%
         error_if(!(ProductTestRaw[,each_var] %in% 1:7),show=c(id,cell))  %>%
         report
}

gdemin/cleanr documentation built on May 16, 2019, 11:14 p.m.