Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
7
Task/Scope-modifiers/R/scope-modifiers-1.r
Normal file
7
Task/Scope-modifiers/R/scope-modifiers-1.r
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
X <- "global x"
|
||||
f <- function() {
|
||||
x <- "local x"
|
||||
print(x) #"local x"
|
||||
}
|
||||
f() #prints "local x"
|
||||
print(x) #prints "global x"
|
||||
5
Task/Scope-modifiers/R/scope-modifiers-2.r
Normal file
5
Task/Scope-modifiers/R/scope-modifiers-2.r
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
d <- data.frame(a=c(2,4,6), b = c(5,7,9))
|
||||
attach(d)
|
||||
b - a #success
|
||||
detach(d)
|
||||
b - a #produces error
|
||||
37
Task/Scope-modifiers/R/scope-modifiers-3.r
Normal file
37
Task/Scope-modifiers/R/scope-modifiers-3.r
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
x <- "global x"
|
||||
print(x) #"global x"
|
||||
|
||||
local({ ## local({...}) is a shortcut for evalq({...}, envir=new.env())
|
||||
## and is also equivalent to (function() {...})()
|
||||
|
||||
x <- "outer local x"
|
||||
print(x) #"outer local x"
|
||||
x <<- "modified global x"
|
||||
print(x) #"outer local x" still
|
||||
y <<- "created global y"
|
||||
print(y) #"created global y"
|
||||
local({
|
||||
|
||||
## Note, <<- is _not_ a global assignment operator. If an
|
||||
## enclosing scope defines the variable, that enclosing scope gets
|
||||
## the assignment. This happens in the order of evalution; a local
|
||||
## variable may be defined later on in the same scope.
|
||||
|
||||
x <- "inner local x"
|
||||
print(x) #"inner local x"
|
||||
x <<- "modified outer local x"
|
||||
print(x) #"inner local x"
|
||||
y <<- "modified global y"
|
||||
print(y) #"modified global y"
|
||||
y <- "local y"
|
||||
print(y) #"local y"
|
||||
|
||||
##this is the only way to reliably do a global assignment:
|
||||
assign("x", "twice modified global x", globalenv())
|
||||
print(evalq(x, globalenv())) #"twice modified global x"
|
||||
})
|
||||
|
||||
print(x) #"modified outer local x"
|
||||
})
|
||||
print(x) #"twice modified global x"
|
||||
print(y) #"modified global y"
|
||||
11
Task/Scope-modifiers/R/scope-modifiers-4.r
Normal file
11
Task/Scope-modifiers/R/scope-modifiers-4.r
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
x <- "global x"
|
||||
f <- function() {
|
||||
cat("Lexically enclosed x: ", x,"\n")
|
||||
cat("Lexically enclosed x: ", evalq(x, parent.env(sys.frame())),"\n")
|
||||
cat("Dynamically enclosed x: ", evalq(x, parent.frame()),"\n")
|
||||
}
|
||||
|
||||
local({
|
||||
x <- "local x"
|
||||
f()
|
||||
})
|
||||
12
Task/Scope-modifiers/R/scope-modifiers-5.r
Normal file
12
Task/Scope-modifiers/R/scope-modifiers-5.r
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
d <- data.frame(a=c(2,4,6), b = c(5,7,9))
|
||||
also <- c(1, 0, 2)
|
||||
with(d, mean(b - a + also)) #returns 4
|
||||
|
||||
## with() is built in, but you might have implemented it like this:
|
||||
|
||||
with.impl <- function(env, expr) {
|
||||
env <- as.environment(env)
|
||||
parent.env(env) <- parent.frame()
|
||||
eval(substitute(expr), envir=env)
|
||||
}
|
||||
with.impl(d, mean(b - a + also))
|
||||
Loading…
Add table
Add a link
Reference in a new issue