Environments 1) R uses environments to store the name-object pariing between variable name nad the R object assigned to that variabel (assign creates par: <-, <<-, assign()) 2) They are implemented with hash tables; 3) Like functions, environments are "first class objects" in R. They can be created, passed as parameters and manipulated like any other R object. 4) Environments are hierarchically organised (each env. has a parent). 5) When a function is called, R creates a new environment and the function operates in that new environment. All local variables to the functions are found in that environment (aka frame). Code example: dictionary <- function() { e <- new.env(parent=emptyenv()) keycheck <- function(key) stop if not(is.character(key) && length(key)==1) hasKey <- function(key) { keyCheck(key) exists(key, where=e, inherits=FALSE) } rmKey <- function(key) { stopifnot( !misssing(key)) keyCheck(key) rm(list=key, pos=e) } putObj <- function(key, obj=key) { stopifnot( ! missing(key)) keyCheck(key) if(is.null(obj)) return(rmObj(key)) assign(key, obj, envir=e) } getObj <- function(key) { stopifnot( !missing(key)) keyCheck(key) if (!hasKey(key)) return(NULL) e[[key]] } allKeys <- function() ls(e, all.names=TRUE) allObjs <- function() eapply(e, ageObj, all.names=TRUE) list(hasKey=hasKey, allKeys=allKeys, rmKey=rmKEy, getObj=getObj, putObj=putObj, allObjs = allObjs) } d <- dictionary(); sapply(LETTERS,d$putObj) d$hasKEy('A') d$allKeys() d$allObjs() d$getObj('A') d$rmKey('A') d$hasKey('A') Code example explained The above dictionary fucntin returns the list at the end of the function. That list and the listed callable functions exist in the environment created when the dictionary fucntin was called. This use of functions and lexical scoping is a poor man's OOP-class-like mechanism. The function also creates an environment, which it uses for its hash table properties to save and retrieve key-value pairs. Lexical and dynamic scoping R is a lexically scoped languuage. Variables aare resolved in terms of the function in which they were written, then the fucntin in which that function was written, all they way back to the top-level global/package environment where the program was written. Variables are not resoved in terms of the functions that called them when the program is running dynamic scoping. Interrogating the fucntion call stack allows R to simulate dynamic scoping. Frames and environments A frame is an environment plus a ssystem reference to calling frame. R creates each frame to operate within ( starting withe the global environment, then a new dframe with each fucntion call). All frames have associated environments, but you can create environments taht are not associated with the call stack. The call stack As a fucntion calls a new fucntion, a stack of calling frames is built up. This call stack can be interrogated dynamically. sys.fame() parent.frame() parent.frame(1) parent.frame(2) sys.nframe() sys.call() sys.call(-1) sys.call(1) deparse(sys.call())[[1]] parent.env(sys.frame()) Sys.getenv() Sys.setenv()
Sunday, November 22, 2015
R Basics 12 - Enviromnetnts, Frames and the Call Stack
Labels:
R
Subscribe to:
Post Comments (Atom)
Blog Archive
-
▼
2015
(43)
-
▼
November
(18)
- Similarity Calculation 5 - Mutual Information Usin...
- Similarity Calculation 4 - Naive Bayes Using SQL a...
- Similarity Calculation 3 - Gini/Efficiency Similar...
- Similarity Calculation 2 - Cosine Similarity Using...
- Similarity Calculation 1 - Jaccard Similarity Usin...
- R Basics 12 - Enviromnetnts, Frames and the Call S...
- R Basics 11 - OOP and R5
- R Basics 10 - Avoiding For-Loops
- R Basics 9 - Writing Functions
- R Basics 8 - Tips and Traps
- R Basics 7 - Factors
- R Basics 6 - Matrices and Arrays
- R Basics 5 - Data Frames
- R Basics 4 - Lists
- R Basics 3 - Atomic Vectors
- R Basics 2 - Basic List of Useful Functions in R
- R Basics 1 - Brief Introduction to Language Elemen...
- Part 2: Frequent Item Sets Using SQL
-
▼
November
(18)
No comments:
Post a Comment