Why do I get two different results if I print x$val? I get that the first one is a list and the second is an environment, but I do not understand what makes the result of x$val from the second chunk = NA
x <- list()
x$val <- 1
myfun <- function(x) {x$val <- x$val NA}
myfun(x)
x <- new.env()
x$val <- 18
myfun <- function(x) {x$val <- x$val NA}
myfun(x)
CodePudding user response:
Environments are "reference objects" in R. That means that if you assign one to a new variable, changes to either copy will affect both copies. Lists are like most other objects and get copied on assignment.
So in your first example, myfun(x) makes a separate copy of the list x, and works on that in the function. It has no effect on the global variable x.
In your second example, myfun(x) makes a new reference to the environment x, and works on that in the function. That affects the original variable as well.
CodePudding user response:
Ok, based on the legit comment of the risk of using <<- some examples.
risky solution
x <- list("val" = 1L)
myfun <- function(x) {x$val <<- x$val NA}
myfun(x)
x
# $val
# [1] NA
risky solution going terribly wrong
x <- list("val" = "Please do not alter me at all")
y <- list("val" = 1L)
myfun <- function(x) {x$val <<- x$val NA}
myfun(y)
x
# $val
# [1] NA
y
# $val
# [1] 1
