[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  eval: Evaluate an (Unevaluated) Expression

##  Description

Evaluate an  R  expression in a specified environment.

##  Usage

    
    
    eval(expr, envir = parent.frame(),
               enclos = if(is.list(envir) || is.pairlist(envir))
                           parent.frame() else baseenv())
    evalq(expr, envir, enclos)
    eval.parent(expr, n = 1)
    local(expr, envir = new.env())

##  Arguments

expr

an object to be evaluated. See ‘Details’.

envir

the ` [ environment ](/link/environment?package=base&version=3.6.2) ` in which
` expr ` is to be evaluated. May also be ` NULL ` , a list, a data frame, a
pairlist or an integer as specified to ` [ sys.call
](/link/sys.call?package=base&version=3.6.2) ` .

enclos

Relevant when ` envir ` is a (pair)list or a data frame. Specifies the
enclosure, i.e., where  R  looks for objects not found in ` envir ` . This can
be ` NULL ` (interpreted as the base package environment, ` [ baseenv
](/link/baseenv?package=base&version=3.6.2) () ` ) or an environment.

n

number of parent generations to go back

##  Value

The result of evaluating the object: for an expression vector this is the
result of evaluating the last element.

##  Details

` eval ` evaluates the ` expr ` argument in the environment specified by `
envir ` and returns the computed value. If ` envir ` is not specified, then
the default is ` [ parent.frame
](/link/parent.frame?package=base&version=3.6.2) () ` (the environment where
the call to ` eval ` was made).

Objects to be evaluated can be of types ` [ call
](/link/call?package=base&version=3.6.2) ` or ` [ expression
](/link/expression?package=base&version=3.6.2) ` or [ name
](/link/name?package=base&version=3.6.2) (when the name is looked up in the
current scope and its binding is evaluated), a [ promise
](/link/promise?package=base&version=3.6.2) or any of the basic types such as
vectors, functions and environments (which are returned unchanged).

The ` evalq ` form is equivalent to ` eval(quote(expr), …) ` . ` eval `
evaluates its first argument in the current scope before passing it to the
evaluator: ` evalq ` avoids this.

` eval.parent(expr, n) ` is a shorthand for ` eval(expr, parent.frame(n)) ` .

If ` envir ` is a list (such as a data frame) or pairlist, it is copied into a
temporary environment (with enclosure ` enclos ` ), and the temporary
environment is used for evaluation. So if ` expr ` changes any of the
components named in the (pair)list, the changes are lost.

If ` envir ` is ` NULL ` it is interpreted as an empty list so no values could
be found in ` envir ` and look-up goes directly to ` enclos ` .

` local ` evaluates an expression in a local environment. It is equivalent to
` evalq ` except that its default argument creates a new, empty environment.
This is useful to create anonymous recursive functions and as a kind of
limited namespace feature since variables defined in the environment are not
visible from the outside.

##  References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S Language_ .
Wadsworth & Brooks/Cole. ( ` eval ` only.)

##  See Also

` [ expression ](/link/expression?package=base&version=3.6.2) ` , ` [ quote
](/link/quote?package=base&version=3.6.2) ` , ` [ sys.frame
](/link/sys.frame?package=base&version=3.6.2) ` , ` [ parent.frame
](/link/parent.frame?package=base&version=3.6.2) ` , ` [ environment
](/link/environment?package=base&version=3.6.2) ` .

Further, ` [ force ](/link/force?package=base&version=3.6.2) ` to _force_
evaluation, typically of function arguments.

##  Examples

[ Run this code
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Feval&utm_source=r-docs&utm_medium=docs&utm_term=eval&utm_content=run_exam