Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

66%
+2 −0
Q&A How can I assign the result of an operation from within a function to the global environment?

In order to assign a variable to the global environment from within a function, there are two ways to proceed: either using the assign operator <<- or using assign(). <<- comes with the...

posted 3y ago by Zerotime‭

Answer
#1: Initial revision by user avatar Zerotime‭ · 2020-10-04T10:55:31Z (over 3 years ago)
In order to assign a variable to the global environment from within a function, there are two ways to proceed: either using the assign operator `<<-` or using `assign()`.

`<<-` comes with the following behaviour: It goes through all environments until it finds the variable in question and replaces it with the current value or creates it anew if it's not found. (Everything run in R 4.0.)

```
> global_x <- 1
> global_x
[1] 1
> meanFUN <- function(x) {
+  global_x <<- mean(x, na.rm = TRUE)
+ }
> meanFUN(c(1:5))
> global_x
[1] 3
```

This will create a new variable called `global_x` in the global environment from which it can be accessed and `global_x` will be changed each time the function is run. However, if `global_x` is found in an environment before the global environment, it will be changed there. If we have a function inside a function with the same variable name across different environments and use the `<<-` operator, `<<-` will only change the first occurrence of the variable in question, so to speak in the overarching local environment of the outer function and not in the global environment.

```
> global_x <- 1
> global_x
[1] 1
> meanFUN <- function(x) {
+   global_x <- 100
+ 
+   insideFunction <- function(x) {
+     local_x <- mean(x, na.rm = TRUE)
+     assign("global_x", local_x, envir = .GlobalEnv)
+     print(global_x)
+   }
+ 
+   print(global_x)
+   insideFunction(x)
+ }
> meanFUN(c(1:5))
[1] 100
[1] 3
> global_x
[1] 1
> 
```

To avoid mistakes while using `<<-`, it's better to use `assign()` which gives precise control over what is assigned where.

```
> global_x <- 1
> global_x
[1] 1
> meanFUN <- function(x) {
+    global_x <- 100
+   
+     insideFunction <- function(x) {
+       global_x <- mean(x, na.rm = TRUE)
+       assign("global_x", global_x, envir = .GlobalEnv)
+       print(global_x)
+     }
+   
+     print(global_x)
+     insideFunction(x)
+   }
> meanFUN(c(1:5))
[1] 100
[1] 3
> global_x
[1] 3
```

On Stack Overflow is a similar question where it's explained in even greater detail: https://stackoverflow.com/a/10904810/3884967.