diff --git a/DESCRIPTION b/DESCRIPTION index 5fb05bb4..bc444832 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -68,7 +68,8 @@ Suggests: rstudioapi, tidyselect, renv, - yaml + yaml, + gt VignetteBuilder: knitr Config/testthat/edition: 3 Depends: diff --git a/NEWS.md b/NEWS.md index 32cdb556..9ef64fd7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,8 @@ ## Updates +- Invisibly returned objects are no longer captured in the "Result" section of the log (#275) + - Removed .dcf file for old Addin (#280) ## Documentation diff --git a/R/writer.R b/R/writer.R index 6f94b114..a22768d4 100644 --- a/R/writer.R +++ b/R/writer.R @@ -313,8 +313,10 @@ write_result <- function(file) { if (is_rmarkdown(file)) { c("\nResult:", paste0("\t", capture.output(result))) - } else { + } else if (isTRUE(result$visible)) { c("\nResult:", paste0("\t", capture.output(result$value))) + } else { + character(0) } } diff --git a/tests/testthat/ref/invisible_returned_gt.R b/tests/testthat/ref/invisible_returned_gt.R new file mode 100644 index 00000000..9ae88eea --- /dev/null +++ b/tests/testthat/ref/invisible_returned_gt.R @@ -0,0 +1,4 @@ +library(gt) + +gt_tbl <- gt::gt(head(mtcars)) +gt_tbl diff --git a/tests/testthat/ref/invisible_returned_obj.R b/tests/testthat/ref/invisible_returned_obj.R new file mode 100644 index 00000000..bd0714e1 --- /dev/null +++ b/tests/testthat/ref/invisible_returned_obj.R @@ -0,0 +1,4 @@ +my_fun <- function(x) { + return(invisible(x)) +} +my_fun("do not print this") diff --git a/tests/testthat/ref/invisible_returned_obj.Rmd b/tests/testthat/ref/invisible_returned_obj.Rmd new file mode 100644 index 00000000..ebafbb34 --- /dev/null +++ b/tests/testthat/ref/invisible_returned_obj.Rmd @@ -0,0 +1,11 @@ +--- +title: "invisible_returned_obj" +output: html_document +--- + +```{r} +my_fun <- function(x) { + return(invisible(x)) +} +my_fun("do not print this") +``` diff --git a/tests/testthat/test-invisible.R b/tests/testthat/test-invisible.R new file mode 100644 index 00000000..a903cb1d --- /dev/null +++ b/tests/testthat/test-invisible.R @@ -0,0 +1,67 @@ +test_that("invisibly returned results are not written to the log", { + options("log.rx" = NULL) + scriptPath <- test_path("ref", "invisible_returned_obj.R") + logDir <- tempdir() + + axecute(scriptPath, log_name = "log_invisible", log_path = logDir) + + con <- file(file.path(logDir, "log_invisible"), "r") + flines <- readLines(con) + close(con) + + expect_false(any(grepl("do not print this", flines))) + + rm(flines, con, logDir) + log_remove() +}) + +test_that("invisibly returned results in Rmd files are not written to the log", { + options("log.rx" = NULL) + scriptPath <- test_path("ref", "invisible_returned_obj.Rmd") + logDir <- tempdir() + + axecute(scriptPath, log_name = "log_invisible_rmd", log_path = logDir) + + con <- file(file.path(logDir, "log_invisible_rmd"), "r") + flines <- readLines(con) + close(con) + + expect_false(any(grepl("do not print this", flines))) + + rm(flines, con, logDir) + log_remove() +}) + +test_that("visibly returned results are written to the log", { + options("log.rx" = NULL) + scriptPath <- test_path("ref", "safely_loudly_test_file_result.R") + logDir <- tempdir() + + axecute(scriptPath, log_name = "log_visible", log_path = logDir) + + con <- file(file.path(logDir, "log_visible"), "r") + flines <- readLines(con) + close(con) + + expect_true(any(grepl("8, 6, 7, 5, 3, 0, 9|test", flines))) + + rm(flines, con, logDir) + log_remove() +}) + +test_that("gt table HTML content is not written to the result portion of the log", { + options("log.rx" = NULL) + scriptPath <- test_path("ref", "invisible_returned_gt.R") + logDir <- tempdir() + + axecute(scriptPath, log_name = "log_gt", log_path = logDir) + + con <- file(file.path(logDir, "log_gt"), "r") + flines <- readLines(con) + close(con) + + expect_false(any(grepl("