knitr::opts_chunk$set(echo = TRUE)

Function environment and Execution Environment

greeting <- function(username){
  cat("Hi, ", username)
}

call is an expression when a function's input arguments' values are supplied so that it is ready to go.

greeting # not a call
greeting("John") # is a call

As we can see that function class object can have different statuses, that is different statuses are called different modes. When object has only static status, its class and mode are the same.

Execution Environment (EE)

Falling into the rabbit hole:

greeting <- function(username){
  browser() # this is the rabbit hole we create to lead us to the execution environment world.
  cat("Hi, ", username)
}
greeting("John")

Function environment

When things are missing in EE

greeting <- function(username){
  cat("Hi, ", username,". I live in ", location, ". ")
}
greeting("John")
debug(greeting) # another way to create the rabbit hole.
greeting("John")
location <- "Sanxia"
greeting("John")

location does not have to exist when the function is created---only needed when the function is called (Lazy Evaluation).

Exist mean exist in Functional Environment (FE) that is where greeting resides, or exist in Execution Environment (EE).

  • EE -> FE

Scoping rule

Genealogy

When something is missing in an environment, how is the program looking for the missing piece? The process of missing piece looking is called scoping.

greetingPersonFromSameLocation <- function(username){
  browser()
  greeting2 <- function(username){
    browser()
    cat("Hi, ", username,". I live in ", location, ". ")
  }
  greeting2(username)
}
location <- "Taipei"
debug(greetingPersonFromSameLocation)
greetingPersonFromSameLocation("John")


tpemartin/econDV1091 documentation built on Feb. 17, 2021, 6:37 a.m.