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 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.
- 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
- Now when we run it from the command prompt, we get.
- 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
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.
- When we call it,
- 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.
Read Also
Global and local variablesAccessing 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.
- On running this function,
- 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().