What is the expression() function in R?
Overview
The expression() function in R creates an expression from the arguments passed to it.
Syntax
expression(x)
Syntax for the expression() function
Parameter value
The expression() function takes one parameter value x. This represents an R object, such as a call, symbol, or constant.
Return value
The expression() function returns a vector of the type expression. This contains its arguments.
Code example
# Creating an expression using the expression() functionmyexpression <- expression(1 + 2)# Obtaining the result of the expression using the eval() functioneval(myexpression)# Checking the class of the objectclass(myexpression)
Explanation
- Line 2: We create an expression variable
myexpressionusing theexpression()function. - Line 5: We evaluate the expression using the
eval()function, and print its result. - Line 8: We check for the class of the object
myexpressionusing theclass()function.