Learn R Programming

session (version 1.0.3)

texteval: Evaluate string(s) containing R commands and return the text transcript or printed results

Description

Evaluate string(s) containing R commands and return the text transcript or printed results

Usage

capture(expression, collapse=NULL)
texteval(sourceText, collapse=NULL, echo=TRUE)
printed(sourceText, collapse=NULL)

Arguments

expression
R expression to be evaluated
sourceText
Vector of string to be evaluated.
collapse
Line separator. Defaults to NULL
echo
Should commands be shown in output. Defaults to TRUE

Value

  • A single character string if collapse is non-NULL, otherwise a vector of character strings.

Details

capture captures the results of executing expression using a textConnection. texteval and printed parse and evaluate the contents of sourceText using source and the results are captured using a textConnection. If collapse is NULL, a vector of strings is returned, one element for each line of output. (Empty strings for blank lines). If collapse is non-NULL, the a single character string is formed by pasting the individuals elements together separated by this value. When echo is TRUE, texteval will return a transcript that includes both printed output and evaluated commands. When echo is FALSE, texteval will return only the printed output. printed always returns only the printed output.

These functions were created to allow strings provided from external processes (for example by rpy or RSPerl) to be evaluated as if they were scripts.

See Also

source, textConnection, sink, parse, eval

Examples

Run this code
# define a script string
script <- "x <- rnorm(100)
y <- x + rnorm(100,0.25)
summary(lm(y~x))"

# evaluate the script string, returning a transcript.
result <- texteval(script, "")
cat(result)

# evaluate the script string, returning the printed output.
result <- printed(script, "")
cat(result)

Run the code above in your browser using DataLab