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.

Comments on How can I assign the result of an operation from within a function to the global environment?

Parent

How can I assign the result of an operation from within a function to the global environment?

+2
−0

I have a function that which does some calculations. I would like to assign the result of the function to the global environment from within the function, how do I proceed?

A minimal example:

meanFUN <- function(x) {
 mean(x, na.rm = TRUE)
}

How can I assign the result from mean(x, na.rm = TRUE) from within the function to the global environment?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

General comments (1 comment)
Post
+2
−0

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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

2 comment threads

Improving your answer (2 comments)
`<<-` should be/can be avoided most of the times. Assigning to a variable outside of the function, as... (1 comment)
Improving your answer
djm‭ wrote about 1 year ago

You end up saying assign() is safer. You might want to put that first, and leave the <<- super-assignment operator for later. The fact that <<- assigns to the global environment when it doesn't find the variable in parent environments is more of a design flaw than a good thing.

djm‭ wrote about 1 year ago

Another improvement: you say <<- "goes through all environments", but that's not quite right. It goes through the chain of parent environments to the function evaluation environment. If it finds an unlocked variable matching the name you are assigning to, it assigns there. (Package variables are locked and won't be modified this way even if they are in the chain.) If it doesn't find a suitable variable, then it creates a new one in the global environment.