scope - R: How can a function assign a value to a variable that will persist outside the function? -
this easy confused hell environments. use call function assign value variable, need able use variable after call. however, assume, variable created function exist within function environment. in short, need akin global variable (but beginner r).
the following code :
assignvalue = function(varname){ assign(varname,1) } assignvalue("test") test
returns error: object 'test' not found
whereas equivalent
test <- 1 test
i read in documentation of assign
environments, not understand it.
say foo
parameter of function. this:
assignvalue <- function(foo) { varname <<- foo } assignvalue("whatever") varname
the variable varname
point out value "whatever"
.
Comments
Post a Comment