When we use some symbol to implement our logics, how does R know which value to assign?
R functions are treated much like any other R objects.
Assume we are running the following code,
If we observe properly, we can still use c() to create vectors. Now the question is how does R know to use which c symbol to use and when? It’s because,
R has separate namespace for functions and non-functions.
Lets try to understand it clearly. When R tried to ‘bind/connect’ a value to a symbol (in our case c(), it search for the corresponding symbol in an order.
- Search the global environment (workspace) for a symbol name matching the request.
- Search the namespaces of each of the packages on the search list.
So the search list will have the follow packages (packages which we have loaded) including “.GlobalEnv” as a first item in the search list and the “base” is always at the very end.
“.GlobalEnv” is just our workspace. If there is a symbol matching it will get it from workspace. If nothing found it will search from the rest of the namespace in each of the packages.
Rules of scoping
R uses scoping rules called Lexical scoping (static scoping).
It will determine the value associated with free variable function.
In the above function we have two arguments and they are x, y. But inside the function body we can find another symbol ‘z’. In this case z is called free variable.
According to scoping rules in R it first searches in the environment where the function was defined. An environment is collection of symbols and values. Environments have patents.
Since we defined fun function in global environment, R will look for z in that scope (environment).
These rule are matters because we can define some complex logics and function.