1 Goal


The goal of this tutorial is to create a new environment to hide all the functions that we have defined in the config.R file of our system.


2 Why hide the functions


The functions that we create in a config.R file are loaded to the Global Environment when sourced directly. This can lead to problems because we could delete variables and functions loaded from that file without noticing and create problems.

If we load these functions and varibales into a new Environment and attach it to the main environment we protect those functions from the user.


3 Create Environment


# In order to do this we just need to create a new environment and attach it to the Global Environment
# Loading Environment
myEnv <- new.env()
sys.source("config.R", envir = myEnv)
attach(myEnv)

4 Explore Environment


# Inside the new environment we can find objects and functions
ls(envir = myEnv)
## [1] "auto.message" "Readme.Env"
# Let's see what this function does
Readme.Env()
## [1] "This environment was created by Luis Serra"

5 Conclusion


In this tutorial we have learnt how to create and explore a new environemnt in order to load our functions and constants.