R - Scope of Variables
A variable can only be accessed in the area in which it is defined, which is called scope of the variable. In R, there are mainly two types of variable scopes:
- Local variable
- Global variable
Local Variable
If a variable is created inside a function, it is called local variable and has local scope. A local variable can only be used inside that function.
Example
In the example below, a local variable MyStr is created and used inside a function to print it.
#defining MyPrint function MyPrint <- function(){ MyStr <- "Hello World!" print(MyStr) } MyPrint()
The output of the above code will be:
[1] "Hello World!"
Local variable in a nested function
A local variable can not be used outside the function in which it is defined but it can be used in any function inside that function.
Example
In the example below, a local variable called MyStr is created inside the function called MyFunction. Along with this, a function called MyPrint() is also defined inside MyFunction. The local variable MyStr will be available inside MyPrint().
#defining a nested function MyFunction <- function(){ MyStr <- "Hello World!" MyPrint <- function(){ print(MyStr) } MyPrint() } MyFunction()
The output of the above code will be:
Hello World!
Global Variable
If a variable is created outside a function, it is called global variable and has local scope. A global variable can be used anywhere, inside the function and outside the function.
Example
In the example below, a global variable MyStr is created and used inside the function to print it.
MyStr <- "Hello World!" #defining MyPrint function MyPrint <- function(){ print(MyStr) } MyPrint()
The output of the above code will be:
[1] "Hello World!"
If a variable with same name is created inside the function, it will be a local variable and can be used inside the function only. Any operation performed on local variable will not change the global variable.
Example
In the example below, a variable MyStr is assigned different values inside and outside the function. When the variable is accessed outside the function, it takes global value and its global value remains unaffected by any operation done with local variable.
MyStr <- "Hello World!" #defining MyPrint function MyPrint <- function(){ MyStr <- "Hello John!" print(MyStr) } #variable accessed inside function MyPrint() #variable accessed outside function print(MyStr)
The output of the above code will be:
[1] "Hello John!" [1] "Hello World!"
Global assignment operators
To access or create a global variable inside a function, following operators can be used:
- <<- : left global assignment operator
- ->> : right global assignment operator
Example
In the example below, (<<-) operator is used to modify and create global variable inside a function.
MyStr <- "Hello World!" #defining MyPrint function MyFunction <- function(){ #accessing a global variable MyStr <<- "Hello John!" #creating a new global variable #it is created after function call MyMessage <<- "R programming is fun" } MyFunction() #accessing variables outside function print(MyStr) print(MyMessage)
The output of the above code will be:
[1] "Hello John!" [1] "R programming is fun"