-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathprint.R
More file actions
71 lines (68 loc) · 2.12 KB
/
print.R
File metadata and controls
71 lines (68 loc) · 2.12 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
60
61
62
63
64
65
66
67
68
69
70
71
#' Print diffdf objects
#'
#' Print a nicely formatted version of a diffdf object.
#'
#' @param x A comparison object created by \code{diffdf()}.
#' @param ... Additional arguments (not used).
#' @param row_limit Maximum number of rows to display in difference tables.
#' Use \code{NULL} to show all rows. Default is 10.
#' @param as_string Logical. If \code{TRUE}, returns the printed message as an R
#' character vector instead of printing to the console. Default is \code{FALSE}.
#' @param file A connection or a character string naming the file to print to. If
#' \code{NULL} (the default), output is printed to the console.
#'
#' @examples
#' x <- subset(iris, -Species)
#' x[1, 2] <- 5
#' COMPARE <- diffdf(iris, x)
#' print(COMPARE)
#' print(COMPARE, row_limit = 5)
#' print(COMPARE, file = "output.txt")
#'
#' @export
print.diffdf <- function(x, row_limit = 10, as_string = FALSE, file = NULL, ...) {
if (!is.null(row_limit)) {
assertthat::assert_that(
assertthat::is.number(row_limit),
row_limit > 0,
msg = "row_limit must be a positive integer"
)
}
assertthat::assert_that(
assertthat::is.flag(as_string)
)
COMPARE <- x
if (length(COMPARE) == 0) {
outtext <- "No issues were found!\n"
} else {
start_text <- paste0("Differences found between the objects!\n\n")
end_text <- lapply(COMPARE, function(x) get_print_message(x, row_limit))
end_text <- paste0(unlist(end_text), collapse = "")
outtext <- paste0(start_text, end_text)
}
string_content <- strsplit(outtext, "\n")[[1]]
if (!is.null(file)) {
tryCatch(
{
sink(file)
cat(string_content, sep = "\n")
sink()
},
warning = function(w) {
sink()
warning(w)
},
error = function(e) {
sink()
stop(e)
}
)
return(invisible(COMPARE))
}
if (as_string) {
return(string_content)
} else {
cat(outtext)
return(invisible(COMPARE))
}
}