R Environment - r - learn r - r programming



  • In order to write functions in a proper way and avoid unusual errors, we need to know the concept of environment and scope in R.
r programming environmental variables

r programming environmental variables

R Programming Environment

  • Environment is defined as a collection of objects such as functions, variables etc.
  • An environment is just a place to store variables that is a set of bindings between symbols and objects
  • An environment is created when we first fire up the R interpreter.
  • If we define any variable, then it wil be available only in this environment.
  • The top level environment at the R command prompt is the global environment and called as R_GlobalEnv.
  • Global environment is represented as .GlobalEnv in R codes.
  • The ls() function is used to display what variables and functions are defined in the current environment.
  • In order to get the current environment, use the environment() function.
> a <- 2
> b <- 5
> f <- function(x) x<-0

>ls()
[1] "a""b""f"

>environment()
<environment: R_GlobalEnv>

> .GlobalEnv
<environment: R_GlobalEnv>
  • Here a, b and f are in the R_GlobalEnv environment.
  • Also note that x in the function arguement is not in this global environment. When we define a function, a new environment is created.
  • Create a new environment with new.env() and assign a couple variables.
  • The function f in the above example creates a new environment inside the global environment.
  • Generally, an environment has a frame, which has all the objects defined, and a pointer to the enclosing parent environment.
  • As a consequence, x is in the frame of the new environment created by the function f.
  • This environment will also have a pointer to R_GlobalEnv.

Example: Cascading of environments

f <- function(f_x){
   g <- function(g_x){
print("Inside g")
print(environment())
print(ls())
   }
g(5)
print("Inside f")
print(environment())
print(ls())
}
  • Now when we run it from the command prompt, we get.
>f(6)
[1] "Inside g"
<environment: 0x0000000010c2bdc8>
[1] "g_x"
[1] "Inside f"
<environment: 0x0000000010c2a870>
[1] "f_x""g"

>environment()
<environment: R_GlobalEnv>

>ls()
[1] "f"
  • Here, the function g is defined inside f and it is understandable that they both have different environments with different objects within their respective frames.

R Programming Scope

outer_func<- function(){
   b <- 20
inner_func<- function(){
       c <- 30
   }
}
a <- 10

Global variables

  • Global variables are the variables which live throughout the execution of a program.
  • It can be modified and accessed from any part of the program.
  • Moreover, global variables also depend upon the view of a function.
  • For instance, in the above code, from the perspective of inner_func(), both a and bare global variables.
  • Although, from the perspective of outer_func(), b is a local variable and only a is global variable and the variable c is completely hidden to outer_func().

Local variables

  • In an alternate, Local variables are those variables which exist only within a definite part of a program like a function, and is delivered when the function call ends.
  • In the above example, the variable c is called a local variable.
  • If we assign a value to a variable with the function inner_func(), the change will be only local and cannot be approached outside the function.
  • This is also the same even if names of both global variable and local variables equals.
  • Look at the example which have a function as like.
outer_func<- function(){
   a <- 20
inner_func<- function(){
       a <- 30
print(a)
   }
inner_func()
print(a)
}
  • When we call it,
> a <- 10

>outer_func()
[1] 30
[1] 20

>print(a)
[1] 10
  • Here, the variable a is created locally within the environment frame of both the functions and is not same to that of the global environment frame.

Accessing global variables

  • Global variables can be read but when trying to assign any value to it, a new local variable is created as a substitute.
  • If we need to make assignments to global variables, superassignment operator, <<-, is used.
  • Using this operator within a function, it searches for the variable in the parent environment frame. If the variable is not found it going on searching the next level till it reaches the global environment.
  • And if the variable is still not found, then it is created and assigned at the global level.
outer_func<- function(){
inner_func<- function(){
       a <<- 30
print(a)
   }
inner_func()
print(a)
}
  • On running this function,
>outer_func()
[1] 30
[1] 30
>print(a)
[1] 30
  • When the statement a <- 30 is encountered within inner_func(), it looks for the variable a in outer_func() environment.
  • If the search get fails, it searches in the R_GlobalEnv.
  • Though, a is not defined in this global environment, it is created and assigned there which is now mentioned and printed from within inner_func() as also as outer_func().


Related Searches to R Environment