-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsummary.R
More file actions
59 lines (59 loc) · 1.76 KB
/
summary.R
File metadata and controls
59 lines (59 loc) · 1.76 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#' Summarise Data Checks
#'
#' `summary_dc()` is a function that calculated statistics for how many data
#' checks passed. It's main input is an object of a DataCheckFlagSet class and
#' output is a summary table.
#'
#' @param input_flag Object of a DataCheckFlagSet class.
#' @param fancy Should output be returned in a rst format.
#' @param filtering_dt Should output be returned as a summary table.
#'
#' @return A data.frame or rst table with summary statistics
#'
#' @importFrom knitr kable
#'
#' @examples
#' result <- perform_dc(data_bats)
#' # Fancy summary table (for usage in reports)
#' summary_dc(result)
#' # object of class used for data filtering data.frame
#' summary_dc(result, fancy = FALSE, filtering_dt = TRUE)
#'
#' @export
#'
summary_dc <- function(input_flag, fancy = TRUE, filtering_dt = FALSE) {
res <- lapply(input_flag@flags, function(x) {
if (length(x@result) == 0) {
return(NULL)
} else {
data.frame(
check = x@name,
target = x@target,
passed = sum(x@result, na.rm = TRUE) / length(x@result),
failed = sum(!x@result, na.rm = TRUE) / length(x@result),
missing = mean(is.na(x@result)),
stringsAsFactors = FALSE
)
}
})
res <- do.call(rbind, res)
res <- res[order(res$check, res$target), ]
rownames(res) <- NULL
if (!fancy & !filtering_dt) {
return(res)
} else {
res$passed <- as.character(round(res$passed * 100, 2))
res$failed <- as.character(round(res$failed * 100, 2))
res$missing <- as.character(round(res$missing * 100, 2))
colnames(res) <- c(
"Data Check", "Column (Target)",
"Passed, %", "Failed, %", "Missing,% "
)
if (fancy) {
return(knitr::kable(res, format = "rst"))
}
if (filtering_dt) {
return(res)
}
}
}