Skip to content

Instantly share code, notes, and snippets.

@ctufts
Created April 28, 2016 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctufts/dd83930184e61b4d9ae4b27100306d0c to your computer and use it in GitHub Desktop.
Save ctufts/dd83930184e61b4d9ae4b27100306d0c to your computer and use it in GitHub Desktop.
Example illustrating the deep assignment operator's (<<-) use in R
####################################################
# 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