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() function
myexpression <- expression(1 + 2)
# Obtaining the result of the expression using the eval() function
eval(myexpression)
# Checking the class of the object
class(myexpression)

Explanation

  • Line 2: We create an expression variable myexpression using the expression() 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 myexpression using the class() function.