Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 2 additions & 19 deletions R/diffdf.R
Original file line number Diff line number Diff line change
Expand Up @@ -370,24 +370,7 @@ diffdf <- function(


if (!is.null(file)) {
x <- print(COMPARE, as_string = TRUE)

tryCatch(
{
sink(file)
cat(x, sep = "\n")
sink()
},
warning = function(w) {
sink()
warning(w)
},
error = function(e) {
sink()
stop(e)
}
)
return(invisible(COMPARE))
print(COMPARE, file = file)
}

return(COMPARE)
Expand Down Expand Up @@ -415,7 +398,7 @@ diffdf <- function(
#' @export
diffdf_has_issues <- function(x) {
if (class(x)[[1]] != "diffdf") stop("x is not an diffdf object")
return(length(x) != 0)
length(x) != 0
}


Expand Down
43 changes: 36 additions & 7 deletions R/print.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
#' Print diffdf objects
#'
#' Print nicely formatted version of an diffdf object
#' @param x comparison object created by diffdf().
#' @param ... Additional arguments (not used)
#' @param row_limit Max row limit for difference tables (NULL to show all rows)
#' @param as_string Return printed message as an R character vector?
#' 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, ...) {
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),
Expand All @@ -33,8 +41,29 @@ print.diffdf <- function(x, row_limit = 10, as_string = FALSE, ...) {
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(strsplit(outtext, "\n")[[1]])
return(string_content)
} else {
cat(outtext)
return(invisible(COMPARE))
Expand Down
5 changes: 3 additions & 2 deletions man/get_print_message.issue.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/get_table.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 13 additions & 6 deletions man/print.diffdf.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions tests/testthat/test-print_output.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,43 @@ test_that("print.diffdf errors when given bad inputs", {
print(diff, as_string = c(TRUE, TRUE)),
)
})


test_that("#135 - Writing to file works as expected with row limits", {
f1 <- withr::local_tempfile()
f2 <- withr::local_tempfile()

x2 <- diffdf(
list_of_comparisons[[5]][[1]],
list_of_comparisons[[5]][[2]],
suppress_warnings = TRUE,
file = f1
)

expect_equal(
readLines(f1),
print(x2, as_string = TRUE)
)

print(x2, file = f2)
expect_equal(
readLines(f1),
readLines(f2)
)



f3 <- withr::local_tempfile()

d1 <- tibble(id = seq_len(30), x = 1)
d2 <- tibble(id = seq_len(30), x = 0)

x2 <- diffdf(d1, d2, suppress_warnings = TRUE)
print(x2, file = f3, row_limit = 5)

expect_equal(
readLines(f3),
print(x2, as_string = TRUE, row_limit = 5)
)

})
Loading