Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

capture_output

Capture output to console


Description

Evaluates code in a special context in which all output is captured, similar to capture.output().

Usage

capture_output(code, print = FALSE, width = 80)

capture_output_lines(code, print = FALSE, width = 80)

testthat_print(x)

Arguments

code

Code to evaluate.

print

If TRUE and the result of evaluating code is visible, print the result using testthat_print().

width

Number of characters per line of output. This does not inherit from getOption("width") so that tests always use the same output width, minimising spurious differences.

Details

Results are printed using the testthat_print() generic, which defaults to print(), giving you the ability to customise the printing of your object in tests, if needed.

Value

capture_output() returns a single string. capture_output_lines() returns a character vector with one entry for each line

Examples

capture_output({
  cat("Hi!\n")
  cat("Bye\n")
})

capture_output_lines({
  cat("Hi!\n")
  cat("Bye\n")
})

capture_output("Hi")
capture_output("Hi", print = TRUE)

testthat

Unit Testing for R

v3.0.2
MIT + file LICENSE
Authors
Hadley Wickham [aut, cre], RStudio [cph, fnd], R Core team [ctb] (Implementation of utils::recover())
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.