R Under development (unstable) (2023-12-18 r85702 ucrt) -- "Unsuffered Consequences" Copyright (C) 2023 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > ## Test and learn scoping and environments > ## I need to refine this and add more notes related to whay I learned > > run.test.environments <- FALSE > > if(run.test.environments){ + + ## Scoping, frames and environments + g <- function(a, b, c = 0) a + b + c + print(parent.frame()) + f1 <- function(){ + x <- 1 + print(ls()) + print(ls(envir = parent.frame())) + print(parent.frame()) + f2 <- function(){ + w <- 2 + print(ls()) + print(ls(envir = parent.frame())) + print(parent.frame()) + ans1 <- g(a = w, eval(x)) + f3 <- function(){ + print(ls(envir = parent.frame())) + print(parent.frame()) + print(eval(x)) + z <- eval(ans1) + eval(w) + eval(x) + } + ans1 <- f3() + } + ans <- f2() + ans + } + + } > > proc.time() user system elapsed 0.09 0.06 0.14