Last data update: 2014.03.03

R: Evaluate an R expression and captures the output
captureOutputR Documentation

Evaluate an R expression and captures the output

Description

Evaluate an R expression and captures the output.

Usage

captureOutput(expr, file=NULL, append=FALSE, collapse=NULL, envir=parent.frame())

Arguments

expr

The R expression to be evaluated.

file

A file name or a connection to where the output is directed. Alternatively, if NULL the output is captured to and returned as a character vector.

append

If TRUE, the output is appended to the file or the (unopened) connection, otherwise it overwrites.

collapse

A character string used for collapsing the captured rows. If NULL, the rows are not collapsed.

envir

The environment in which the expression is evaluated.

Details

This method immitates capture.output with the major difference that it captures strings via a raw connection rather than via internal strings. The latter becomes exponentially slow for large outputs [1,2].

Value

Returns captured output as a character vector.

Author(s)

Henrik Bengtsson

References

[1] R-devel thread 'capture.output(): Using a rawConnection() [linear] instead of textConnection() [exponential]?', 2014-02-03. https://stat.ethz.ch/pipermail/r-devel/2014-February/068349.html [2] JottR blog post 'PERFORMANCE: captureOutput() is much faster than capture.output()', 2015-05-26. http://www.jottr.org/2014/05/captureOutput.html

See Also

Internally, eval() is used to evaluate the expression. and capture.output to capture the output.

Examples

# captureOutput() is much faster than capture.output()
# for large outputs when capturing to a string.
for (n in c(10e3, 20e3, 30e3, 40e3)) {
  printf("n=%d\n", n)

  x <- rnorm(n)

  t0 <- system.time({
    bfr0 <- capture.output(print(x))
  })
  print(t0)

  t1 <- system.time({
    bfr <- captureOutput(print(x))
  })
  print(t1)
  print(t1/t0)

  bfr2n <- captureOutput(print(x), collapse="\n")
  bfr2r <- captureOutput(print(x), collapse="
")

  stopifnot(identical(bfr, bfr0))
} # for (n ...)

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

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.7.1 (2016-02-15) successfully loaded. See ?R.methodsS3 for help.
R.oo v1.20.0 (2016-02-17) successfully loaded. See ?R.oo for help.

Attaching package: 'R.oo'

The following objects are masked from 'package:methods':

    getClasses, getMethods

The following objects are masked from 'package:base':

    attach, detach, gc, load, save

R.utils v2.3.0 (2016-04-13) 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, inherits, isOpen, parse, warnings

> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/R.utils/captureOutput.Rd_%03d_medium.png", width=480, height=480)
> ### Name: captureOutput
> ### Title: Evaluate an R expression and captures the output
> ### Aliases: captureOutput
> ### Keywords: IO programming
> 
> ### ** Examples
> 
> # captureOutput() is much faster than capture.output()
> # for large outputs when capturing to a string.
> for (n in c(10e3, 20e3, 30e3, 40e3)) {
+   printf("n=%d\n", n)
+ 
+   x <- rnorm(n)
+ 
+   t0 <- system.time({
+     bfr0 <- capture.output(print(x))
+   })
+   print(t0)
+ 
+   t1 <- system.time({
+     bfr <- captureOutput(print(x))
+   })
+   print(t1)
+   print(t1/t0)
+ 
+   bfr2n <- captureOutput(print(x), collapse="\n")
+   bfr2r <- captureOutput(print(x), collapse="
")
+ 
+   stopifnot(identical(bfr, bfr0))
+ } # for (n ...)
n=10000
   user  system elapsed 
  0.024   0.000   0.024 
   user  system elapsed 
  0.008   0.000   0.010 
     user    system   elapsed 
0.3333333       NaN 0.4166667 
n=20000
   user  system elapsed 
  0.068   0.004   0.072 
   user  system elapsed 
  0.020   0.000   0.018 
     user    system   elapsed 
0.2941176 0.0000000 0.2500000 
n=30000
   user  system elapsed 
  0.160   0.000   0.164 
   user  system elapsed 
  0.028   0.000   0.027 
     user    system   elapsed 
0.1750000       NaN 0.1646341 
n=40000
   user  system elapsed 
  0.280   0.000   0.281 
   user  system elapsed 
  0.036   0.000   0.035 
     user    system   elapsed 
0.1285714       NaN 0.1245552 
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>