withCapture | R Documentation |
Evaluates an expression and captures the code and/or the output.
withCapture(expr, replace=getOption("withCapture/substitute", ".x."), code=TRUE,
output=code, ..., max.deparse.length=getOption("max.deparse.length", 10000), trim=TRUE,
newline=getOption("withCapture/newline", TRUE), collapse="\n", envir=parent.frame())
expr |
The R expression to be evaluated. |
replace |
An optional named |
code |
If |
output |
If |
... |
Additional arguments passed to |
max.deparse.length |
A positive |
trim |
If |
newline |
If |
collapse |
A |
envir |
The |
Returns a character
string class 'CapturedEvaluation'.
Henrik Bengtsson
Internally, eval
() is used to evaluate the expression.
print(withCapture({
n <- 3
n
for (kk in 1:3) {
printf("Iteration #%d\n", 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"
# Automatic "variable" substitute
# (disable with relabel=NULL)
a <- 2
b <- "Hello world!"
print(withCapture({
x <- .a.
s <- .b.
x
s
}))
## > x <- 2
## > s <- "Hello world!"
## > x
## [1] 2
## > s
## [1] "Hello world!"
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.