R Under development (unstable) (2023-11-16 r85542 ucrt) -- "Unsuffered Consequences" Copyright (C) 2023 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library("R.utils") Loading required package: R.oo Loading required package: R.methodsS3 R.methodsS3 v1.8.2 (2022-06-13 22:00:14 UTC) successfully loaded. See ?R.methodsS3 for help. R.oo v1.25.0 (2022-06-12 02:20:02 UTC) successfully loaded. See ?R.oo for help. Attaching package: 'R.oo' The following object is masked from 'package:R.methodsS3': throw The following objects are masked from 'package:methods': getClasses, getMethods The following objects are masked from 'package:base': attach, detach, load, save R.utils v2.12.3 successfully loaded. See ?R.utils for help. Attaching package: 'R.utils' The following object is masked from 'package:utils': timestamp The following objects are masked from 'package:base': cat, commandArgs, getOption, isOpen, nullfile, parse, warnings > > message("*** captureOutput() == capture.output()") *** captureOutput() == capture.output() > for (n in c(0, 1, 10, 100, 1000)) { + printf("n=%d\n", n) + x <- rnorm(n) + str(x) + + bfr0 <- capture.output(print(x)) + bfr <- captureOutput(print(x)) + stopifnot(nchar(bfr) == nchar(bfr0)) + + stopifnot(identical(bfr, bfr0)) + } # for (n ...) n=0 num(0) n=1 num 0.00925 n=10 num [1:10] -0.908 -0.435 -0.256 0.468 -0.134 ... n=100 num [1:100] -0.378 -1.908 0.29 1.266 0.442 ... n=1000 num [1:1000] 1.128 -0.274 0.755 0.263 -0.661 ... > > message("*** captureOutput(..., collapse=ch)") *** captureOutput(..., collapse=ch) > x <- c("abc", "123", "def\n456") > for (ch in list(NULL, "\n", "\r", "\n\r", "\r\n", ";\n", "")) { + bfr0 <- paste(capture.output(cat(x)), collapse=ch) + bfr <- captureOutput(cat(x), collapse=ch) + str(list(bfr0=bfr0, bfr=bfr)) + stopifnot(identical(bfr0, bfr)) + } List of 2 $ bfr0: chr [1:2] "abc 123 def" "456" $ bfr : chr [1:2] "abc 123 def" "456" List of 2 $ bfr0: chr "abc 123 def\n456" $ bfr : chr "abc 123 def\n456" List of 2 $ bfr0: chr "abc 123 def\r456" $ bfr : chr "abc 123 def\r456" List of 2 $ bfr0: chr "abc 123 def\n\r456" $ bfr : chr "abc 123 def\n\r456" List of 2 $ bfr0: chr "abc 123 def\r\n456" $ bfr : chr "abc 123 def\r\n456" List of 2 $ bfr0: chr "abc 123 def;\n456" $ bfr : chr "abc 123 def;\n456" List of 2 $ bfr0: chr "abc 123 def456" $ bfr : chr "abc 123 def456" > > > message("*** captureOutput(..., file='foo.txt')") *** captureOutput(..., file='foo.txt') > x <- c("abc", "123", "def\n456") > capture.output(cat(x), file="foo1.txt") > captureOutput(cat(x), file="foo2.txt") > bfr1 <- readLines("foo1.txt", warn=FALSE) > bfr2 <- readLines("foo2.txt", warn=FALSE) > stopifnot(all.equal(bfr2, bfr1)) > file.remove("foo1.txt") [1] TRUE > file.remove("foo2.txt") [1] TRUE > > > > proc.time() user system elapsed 0.40 0.07 0.46