Skip to content

Commit 19a4dab

Browse files
authored
Add file arguement to print function (#140)
1 parent 3247d7c commit 19a4dab

6 files changed

Lines changed: 96 additions & 35 deletions

File tree

R/diffdf.R

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -370,24 +370,7 @@ diffdf <- function(
370370

371371

372372
if (!is.null(file)) {
373-
x <- print(COMPARE, as_string = TRUE)
374-
375-
tryCatch(
376-
{
377-
sink(file)
378-
cat(x, sep = "\n")
379-
sink()
380-
},
381-
warning = function(w) {
382-
sink()
383-
warning(w)
384-
},
385-
error = function(e) {
386-
sink()
387-
stop(e)
388-
}
389-
)
390-
return(invisible(COMPARE))
373+
print(COMPARE, file = file)
391374
}
392375

393376
return(COMPARE)
@@ -415,7 +398,7 @@ diffdf <- function(
415398
#' @export
416399
diffdf_has_issues <- function(x) {
417400
if (class(x)[[1]] != "diffdf") stop("x is not an diffdf object")
418-
return(length(x) != 0)
401+
length(x) != 0
419402
}
420403

421404

R/print.R

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
#' Print diffdf objects
22
#'
3-
#' Print nicely formatted version of an diffdf object
4-
#' @param x comparison object created by diffdf().
5-
#' @param ... Additional arguments (not used)
6-
#' @param row_limit Max row limit for difference tables (NULL to show all rows)
7-
#' @param as_string Return printed message as an R character vector?
3+
#' Print a nicely formatted version of a diffdf object.
4+
#'
5+
#' @param x A comparison object created by \code{diffdf()}.
6+
#' @param ... Additional arguments (not used).
7+
#' @param row_limit Maximum number of rows to display in difference tables.
8+
#' Use \code{NULL} to show all rows. Default is 10.
9+
#' @param as_string Logical. If \code{TRUE}, returns the printed message as an R
10+
#' character vector instead of printing to the console. Default is \code{FALSE}.
11+
#' @param file A connection or a character string naming the file to print to. If
12+
#' \code{NULL} (the default), output is printed to the console.
13+
#'
814
#' @examples
915
#' x <- subset(iris, -Species)
1016
#' x[1, 2] <- 5
1117
#' COMPARE <- diffdf(iris, x)
1218
#' print(COMPARE)
1319
#' print(COMPARE, row_limit = 5)
20+
#' print(COMPARE, file = "output.txt")
21+
#'
1422
#' @export
15-
print.diffdf <- function(x, row_limit = 10, as_string = FALSE, ...) {
23+
print.diffdf <- function(x, row_limit = 10, as_string = FALSE, file = NULL, ...) {
1624
if (!is.null(row_limit)) {
1725
assertthat::assert_that(
1826
assertthat::is.number(row_limit),
@@ -33,8 +41,29 @@ print.diffdf <- function(x, row_limit = 10, as_string = FALSE, ...) {
3341
end_text <- paste0(unlist(end_text), collapse = "")
3442
outtext <- paste0(start_text, end_text)
3543
}
44+
45+
string_content <- strsplit(outtext, "\n")[[1]]
46+
if (!is.null(file)) {
47+
tryCatch(
48+
{
49+
sink(file)
50+
cat(string_content, sep = "\n")
51+
sink()
52+
},
53+
warning = function(w) {
54+
sink()
55+
warning(w)
56+
},
57+
error = function(e) {
58+
sink()
59+
stop(e)
60+
}
61+
)
62+
return(invisible(COMPARE))
63+
}
64+
3665
if (as_string) {
37-
return(strsplit(outtext, "\n")[[1]])
66+
return(string_content)
3867
} else {
3968
cat(outtext)
4069
return(invisible(COMPARE))

man/get_print_message.issue.Rd

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get_table.Rd

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/print.diffdf.Rd

Lines changed: 13 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-print_output.R

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,43 @@ test_that("print.diffdf errors when given bad inputs", {
117117
print(diff, as_string = c(TRUE, TRUE)),
118118
)
119119
})
120+
121+
122+
test_that("#135 - Writing to file works as expected with row limits", {
123+
f1 <- withr::local_tempfile()
124+
f2 <- withr::local_tempfile()
125+
126+
x2 <- diffdf(
127+
list_of_comparisons[[5]][[1]],
128+
list_of_comparisons[[5]][[2]],
129+
suppress_warnings = TRUE,
130+
file = f1
131+
)
132+
133+
expect_equal(
134+
readLines(f1),
135+
print(x2, as_string = TRUE)
136+
)
137+
138+
print(x2, file = f2)
139+
expect_equal(
140+
readLines(f1),
141+
readLines(f2)
142+
)
143+
144+
145+
146+
f3 <- withr::local_tempfile()
147+
148+
d1 <- tibble(id = seq_len(30), x = 1)
149+
d2 <- tibble(id = seq_len(30), x = 0)
150+
151+
x2 <- diffdf(d1, d2, suppress_warnings = TRUE)
152+
print(x2, file = f3, row_limit = 5)
153+
154+
expect_equal(
155+
readLines(f3),
156+
print(x2, as_string = TRUE, row_limit = 5)
157+
)
158+
159+
})

0 commit comments

Comments
 (0)