What is the eval() function in R?

Overview

The eval() function in R evaluates a given expression and returns the result.

Syntax

eval(expr)
Syntax for the eval() function

Parameter value

expr: This represents the expression to be evaluated.

Return value

The eval() function returns the result of evaluating the object.

Example

# Creating R expressions
a <- expression(3 * 5)
b <- "3 * 5"
# Implementing the eval() function
eval(a)
eval(b)
# Creating a function
myfunction <- function(x, y){x + y}
eval(myfunction(1, 2))

Explanation

  • Lines 2 to 3: We create R expression variables a and b.
  • Lines 6 to 7: We use the eval() function to evaluate the expressions in a and b.
  • Line 11: We create a function myfunction with two parameter values.
  • Line 12: Using the eval() function, we evaluate the expression of the myfunction function containing its required arguments. We print the result to the console.

New on Educative
Learn any Language for FREE all September 🎉
For the entire month of September, get unlimited access to our entire catalog of beginner coding resources.
🎁 G i v e a w a y
30 Days of Code
Complete Educative’s daily coding challenge every day in September, and win exciting Prizes.

Free Resources