R.utils (version 1.16.0)

evalCapture: Evaluates an expression and captures the code and/or the output

Description

Evaluates an expression and captures the code and/or the output.

Usage

evalCapture(expr, code=TRUE, output=code, ..., trim=TRUE, collapse="
", envir=parent.frame())

Arguments

expr
The expression to be evaluated.
...
Additional arguments passed to sourceTo which in turn passes arguments to source().
code
If TRUE, the deparsed code of the expression is echoed.
output
If TRUE, the output of each evaluated subexpression is echoed.
envir
The @enviroment in which the expression is evaluated.
trim
If TRUE, the captured rows are trimmed.
collapse
A character string used for collapsing the captured rows. If NULL, the rows are not collapsed.

Value

  • Returns a character string class 'CapturedEvaluation'.

Examples

Run this code
print(evalCapture({
 n <- 3;
 n;

 for (kk in 1:3) {
   printf("Iteration #%d
", kk);
 }

 print(Sys.time());

 type <- "horse";
 type;
}))

## > n <- 3
## > n
## [1] 3
## > for (kk in 1:3) {
## +     printf("Iteration #\%d\n", kk)
## + }
## Iteration #1
## Iteration #2
## Iteration #3
## > print(Sys.time())
## [1] "2011-11-06 11:06:32 PST"
## > type <- "horse"
## > type
## [1] "horse"

Run the code above in your browser using DataCamp Workspace