Press "Enter" to skip to content

How to differentiate local and global objects in R?

Zigya Acadmey 0

In this article, we’ll learn about the scope of variables and how each of the scope works inside a function and outside a function in R.

Within a function in R, the variables declared are local to that function.

While you’re inside a function, R creates a new environment. By default, it includes everything from the environment in which it was created. We can use those all the variables present outside the function as well but, any variable that is new that we create inside the function, will only be accessible as long as it’s inside the function. Hence, these variables cannot be accessed outside the function.

These variables are therefore referred to as local variables. The program-wide variables that can be accessed are considered Global Variables. One by one, we’ll run over the local and global variables:

Local Variable

Variables defined within a function or block are said to be local to those functions.

  • Local variables do not exist outside the block in which they are declared, i.e. they cannot be accessed or used outside that block.
  • Declaring local variables: Local variables are declared inside a block.

Example

> fun <- function() {
  var1 <- 1
}
> fun()
> var1
Error: object 'var1' not found

Here “var1” can be only accessed by fun(). When we try to access the same variable (“var1”) from outside the function, it gives an error.

Therefore, to correct the error above we have to display the value of the variable “var1” only from inside the function fun().

> fun <- function() {
  var1 <- 1
0
  print(var1)
}
> fun()
[1] 10

Global Variable

Also, the name suggests, Global Variables can be accessed from any part of the program.

  • They are available throughout the lifetime of the program.
  • They are declared anywhere in the program outside all of the functions or blocks.
  • Declaring global variables: Global variables are usually declared outside of all of the functions and blocks. They can be accessed from any portion of the program.

The “<<-“ operator assigns a global value or create a new variable in the global environment even if you’re inside a function. 

The <<- operator checks the parent environment for a variable with the name of the new variable. If it doesn’t find the variable name in the parent environment, it goes to the parent of the parent environment. It continues in the same way till it finds the variable with the same name, otherwise if it isn’t found in the global environment it will assign the variable in the global environment.

For example:

 fun <- function() {
+   var1 <<- 1
+ }
> fun()
> var1
[1] 1

If you want to assign an object to the global environment you can use the assign function and tell it explicitly that you want to assign globally.

For example:

> var1 <- "global"
> fun1 <- function(){
+  var1 <- "in fun1"  
+ 
+ fun2 <- function(){
+   assign("var1", "in fun2", envir = .GlobalEnv)
+   }
+ print(var1)
+ fun2()
+ print(var1)
+ }
> var1
[1] "global"
> fun1()
[1] "in fun1"
[1] "in fun1"
> var1
[1] "in fun2"

In the above example, the assign function did not consider the copy of the var1 inside of fun1 because we mentioned it exactly where to look. However, this time the value of var1 in the global environment was changed because we explicitly assigned it there.

So, now If you want to create a local variable, you can use the local function as follows:

var1 <- "global"
local({
  var1 <- "local"
  print(var1)
})
[1] "local"
> var1
[1] "global"

Conclusion

Hence, we saw difference between global and local variable, also how to assign local and global variable we also saw how to access local variable in global.

This brings the end of this Blog. We really appreciate your time.

Hope you liked it.

Do visit our page www.zigya.com/blog for more informative blogs on Data Science

Keep Reading! Cheers!

Zigya Academy
BEING RELEVANT

Leave a Reply

Your email address will not be published.