R Under development (unstable) (2025-02-23 r87804 ucrt) -- "Unsuffered Consequences" Copyright (C) 2025 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.27.0 (2024-11-01 18:00: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.13.0 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, use, 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.497 n=10 num [1:10] -0.3611 0.1426 -0.1035 0.0158 -1.0348 ... n=100 num [1:100] 1.4728 -0.0607 0.0866 -1.5829 0.3093 ... n=1000 num [1:1000] 0.8809 -1.6896 -1.1789 0.9363 -0.0906 ... > > 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.23 0.01 0.25