R Language

Expression: parse + eval

Remarks#

The function parse convert text and files into expressions.

The function eval evaluate expressions.

Execute code in string format

In this exemple, we want to execute code which is stored in a string format.

# the string
str <- "1+1"

# A string is not an expression.
is.expression(str)
[1] FALSE

eval(str)
[1] "1+1"

# parse convert string into expressions
parsed.str <- parse(text="1+1")

is.expression(parsed.str)
[1] TRUE

eval(parsed.str)
[1] 2

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow