How do i modify the value of vairable 'loco' in following snippet:
poco <- function() {
func <- function(x) {
print(loco)
loco <- loco+x
}
loco <- 123
func(1)
func(2)
}
this functions gives following result:
> poco()
[1] 123
[1] 123
How do i modify the value of vairable 'loco' in following snippet:
poco <- function() {
func <- function(x) {
print(loco)
loco <- loco+x
}
loco <- 123
func(1)
func(2)
}
this functions gives following result:
> poco()
[1] 123
[1] 123
poco <- function() {
func <- function(x) {
print(loco)
loco <<- loco+x
}
loco <- 123
func(1)
func(2)
}
This <<-
operator assigns to the outer scope. (like assign(..., env=...)
). However, as mentioned in the comments, this is usually a bad idea. If you'd like to ask a second question expanding on this where you outline your entire problem, I bet there are other, better choices.
The <<-
can bit you in the butt if you're not careful. See this wiki article
What was happening in your first function where you loco <- loco + x
is that the function looks outside of the scope of func
for loco
when it finds it it brings in into the local scope of func
and assigns to loco
in the local scope rather than the poco
scope.
Hope that helps!
R has a stack of environments. So while you are changing a variable within a function with simple <-
or =
commands, its value will not change in the outer environment.
To do so, you have several options, as illustrated below:
1st Option:
func <- function(x) {
print(loco)
# To modify value of "loco" just in the parent environment, but not globally
loco <<- loco+x
}
2nd (better) Option:
func <- function(x) {
print(loco)
# Again modifies the varaible just in the parent environment, not globally
assign("loco", loco + x, envir = sys.frame(-1))
}
And the 3rd Option:
func <- function(x) {
print(loco)
# To modify the value of a global variable "loco"
assign("loco", loco + x, envir = .GlobalEnv)
}
then you will have:
loco <- 123
func(1) # 123
func(2) # 124
loco # 126
Note that by using options 1 and 2, if you have several nested function definitions, you are modifying the value just in the parent function but not globally.
poco <- function() {
func <- function(loco,x) {
print(loco)
loco <- loco+x
loco
}
loco <- 123
loco <- func(loco,1)
loco <- func(loco,2)
loco
}
loco_final <- poco()
#[1] 123
#[1] 124
loco_final
#[1] 126
In general it is a good thing that loco
is not changed in the function. Not using these kind of global variables ensures that variables do not interfere which each in larger scripts.
Let's say that you use a variable bla
in function_a
and function_b
. Predicting the result of a function call is hard as it depends on the history of bla
. In jargon this is called that the functions have side effects. Not using these makes the functions more predictable, and easier to debug. In addition, when your script grows, you prevent any problems with new functions or code snippets changing bla
and thus changing what happens in the functions.
In general, if you need a variable in a function, pass it as a variable. However, R does allow scoping from inside the function to outside the function, but not vice-versa. See also this recent question for more information.