Created
April 28, 2016 19:56
-
-
Save ctufts/dd83930184e61b4d9ae4b27100306d0c to your computer and use it in GitHub Desktop.
Example illustrating the deep assignment operator's (<<-) use in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#################################################### | |
# case of global assignment | |
# parent environment is the global environment | |
# from Hadley Wickham's 'Advanced R' | |
##################################################### | |
x <- 0 | |
f <- function() { | |
x <<- 1 | |
} | |
f() | |
x | |
#[1] 1 | |
###################################################### | |
# case of non-global assignment | |
# parent assignment is not the global environment | |
##################################################### | |
x <- 0 | |
g <- function(){ | |
x <- 3 | |
print(x) | |
f <- function() { | |
x <<- 1 | |
} | |
f() | |
print(x) | |
} | |
g() | |
#[1] 3 | |
#[1] 1 | |
x | |
#[1] 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment