tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
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"
|
||||
Loading…
Add table
Add a link
Reference in a new issue