Reputation: 59585
In R, when I want to modify global variable in function, I do it like this:
myfun <- function ()
{
a <<- 1
# .. later.. more complex code
a <<- 2
a <<- 3
}
I don't like having to specify <<-
instead <-
all the time. I am not used to it and I always forget. Is it in R somehow possible to declare variable as global just once? E.g. something along the lines:
myfun <- function ()
{
global a
a <- 1 # modify global var
# .. later.. more complex code
a <- 2 # modify global var
a <- 3 # modify global var
}
PS: I do not see how this would be a duplicate of the proposed question. Please expand in an answer to explain.
Upvotes: 1
Views: 69
Reputation: 389325
Why do you need <<-
to update a
all the time in global environment in the function ? Use it only once at the end of the function to update the global variable.
myfun <- function () {
a <- 1
# .. later.. more complex code
a <- 2
a <<- 3
}
Upvotes: 1