Why does evaluating an expression in system.time() make variables available in global environment?

后端 未结 3 1602
挽巷
挽巷 2021-01-11 15:35

Can somebody please explain what happens when an expression is evaluated in system.time? In particular, why are any variables that are declared in the ex

相关标签:
3条回答
  • 2021-01-11 15:43

    It is because supplied arguments are evaluated in the evaluation frame of the calling function (as described in Section 4.3.3 of the R Language Definition document).

    The expression wrapped by the user in system.time() is a supplied argument which gets matched positionally to expr. Then, when expr's evaluation is forced in the body of system.time, it is evaluated in the evaluation frame of the calling function. If system.time() was called from the .GlobalEnv, that is where any assignments that are a part of expr will take place.

    EDIT:

    Here's an example showing that expr is evaluated in the global environment if it is a supplied (but not a default) argument.

    st2 <- function(expr = newVar <- 33){
       expr
    }
    
    # Using the default argument -- eval and assignment 
    # within evaluation frame of the function. 
    st2()
    newVar
    Error: object 'newVar' not found
    
    # Using a supplied argument -- eval and assignment
    # within the calling function's evaluation frame (here .GlobalEnv)
    st2(newVar <- 44)
    newVar
    # [1] 44
    
    0 讨论(0)
  • 2021-01-11 15:58

    EDIT : as per @Tommy's comment: The evaluation actually only takes place once the argument expr is used (that's the lazy evaluation).

    What is passed is a language object, not an expression. You basically nest the <- function (with two arguments) within the st() function call, and the result of the <- call is passed to to st. As you can see in ?assignOps, the <- function returns the assigned value silently. As @Josh told you already, this evaluation of the nested function takes place in the environment the function is called from.

    What you do, is equivalent to

    st(mean(1:10))
    

    To see the difference, you can do:

    st <- function(expr){
      typeof(expr)
    }
    > st(aa <- 1)
    [1] "double"
    > st(expression(aa <- 1))
    [1] "expression"
    

    For the structure of the call, you can do:

    st <- function(expr){
      str(as.list(match.call()))
    }
    > st(mean(1:10))
    List of 2
     $     : symbol st
     $ expr: language mean(1:10)
    > st(aa <- 1)
    List of 2
     $     : symbol st
     $ expr: language aa <- 1
    
    0 讨论(0)
  • 2021-01-11 16:02

    I think the expr is evaluated before handling that to the function. POC example:

    > st <- function(expr){
    +   eval(parse(text=expr))
    + }
    > 
    > st('aa <- 1')
    > aa
    Error: object 'aa' not found
    

    So I think the function gets expr as only aa. Another example:

    > st <- function(expr){
    +   str(expr)
    + }
    > 
    > st(aa <- 1)
     num 1
    

    I might be wrong, it is rather an intuition :) But thanks, that is a good puzzle!


    Update:

    > system.time(a <- 1)
       user  system elapsed 
          0       0       0 
    > a
    [1] 1
    > rm(a)
    > fn <- function() a <- 1
    > system.time(fn())
       user  system elapsed 
          0       0       0 
    > a
    Error: object 'a' not found
    
    0 讨论(0)
提交回复
热议问题