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 expressionsa <- expression(3 * 5)b <- "3 * 5"# Implementing the eval() functioneval(a)eval(b)# Creating a functionmyfunction <- function(x, y){x + y}eval(myfunction(1, 2))
Explanation
- Lines 2 to 3: We create R expression variables
aandb. - Lines 6 to 7: We use the
eval()function to evaluate the expressions inaandb. - Line 11: We create a function
myfunctionwith two parameter values. - Line 12: Using the
eval()function, we evaluate the expression of themyfunctionfunction containing its required arguments. We print the result to the console.