

   eval {base}                                  R Documentation

   EEvvaalluuaattee aann ((UUnneevvaalluuaatteedd)) EExxpprreessssiioonn

   DDeessccrriippttiioonn::

        Evaluate an R expression in a specified environment.

   UUssaaggee::

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

   AArrgguummeennttss::

       expr: object of mode `expression' or`call' or an
             ``unevaluated expression''.

      envir: the `environment' in which `expr' is to be evalu-
             ated.  May also be a list or an integer as in
             `sys.call'.

     enclos: Only relevant if `envir' is a list. Specifies the
             enclosure, i.e., where R looks for objects not
             found in `envir'.

          n: parent generations to go back

   DDeettaaiillss::

        `eval' evaluates the expression `expr' argument in the
        environment specified by `envir' and returns the com-
        puted value.  If `envir' is not specified, then
        `sys.frame(sys.parent())', the environment where the
        call to `eval' was made is used.

        The `evalq' form is equivalent to `eval(quote(expr),
        ...)'.

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

        `local' evaluates an expression in a local environment.
        It is equivalent to `evalq' except the 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.

   NNoottee::

        Due to the difference in scoping rules, there are some
        differences between R and S in this area.  In particu-
        lar, the default enclosure in S is the global environ-
        ment.

        When evaluating expressions in dataframes that has been
        passed as argument to a function, the relevant enclo-
        sure is often the caller's environment, i.e., one needs
        `eval(x, data, sys.frame(sys.parent()))'.

   SSeeee AAllssoo::

        `expression', `quote', `sys.frame', `environment'.

   EExxaammpplleess::

        eval(2 ^ 2 ^ 3)
        mEx <- expression(2^2^3); mEx; 1 + eval(mEx)
        eval({ xx <- pi; xx^2}) ; xx

        a <- 3 ; aa <- 4 ; evalq(evalq(a+b+aa, list(a=1)), list(b=5)) # == 10
        a <- 3 ; aa <- 4 ; evalq(evalq(a+b+aa, 1), list(b=5))         # == 12

        ev <- function() {
             e1 <- sys.frame(sys.parent())
             ## Evaluate a in e1
             aa <- eval(expression(a),e1)
             ## evaluate the expression bound to a in e1
             a <- expression(x+y)
             list(aa = aa, eval = eval(a, e1))
        }
        tst.ev <- function(a = 7) { x <- pi; y <- 1; ev() }
        tst.ev()#-> aa : 7,  eval : 4.14

        ##
        ## Uses of local()
        ##

        # Mutual recursives.
        # gg gets value of last assignment, an anonymous version of f.

        gg<-local({
            k<-function(y)f(y)
            f<-function(x)if(x)x*k(x-1)else 1
        })
        gg(10)
        sapply(1:5,gg)

        # Nesting locals. a is private storage accessible to k
        gg<-local({
            k<-local({
                a<-1
                function(y){print(a<<-a+1);f(y)}
            })
            f<-function(x)if(x)x*k(x-1)else 1
        })
        sapply(1:5,gg)

        ls(envir=environment(gg))
        ls(envir=environment(get("k",envir=environment(gg))))

