R Programming

Environment system in R programming

We provide effective and economically affordable training courses for R and Python, click here for more details and course registration !

R’s environment system behaves as a hierarchical folder system where objects in an R session are located. So when a R session is started, and with several objects created, the environment hierarchy in R session can be shown with function parenvs(). For exmple, in the following code, we create several objects, and then create some plots with ggplot2 package, then do some regression analysis and show the current environment system in R working session.

#load package 'pryr' where parenvs() function is stored there
library(pryr)
#to create two objects
obj1 <- c(1,2,3)
obj2 <- c("one","two","three")

#load library 'ggplot2' for plotting
library(ggplot2)
#to set working directory
setwd("d:\\RStatistics-Tutorial")  
##set colClasses in read.table() to create a data frame
vartype<-c("numeric","numeric" ,"character", "character", "character", "character", "numeric","numeric", "character")
#create a data frame 'Income' by reading a csv file from working directory
Income <- read.table("Income.csv", colClasses=vartype, header=TRUE, sep=",")  

#to set variable types in data frame
Income$Race<-as.factor(Income$Race)
Income$Gender<-as.factor(Income$Gender)
Income$Region<-as.factor(Income$Region)
Income$Married<-as.factor(Income$Married)
Income$Job<-as.factor(Income$Job)

# --  Creating a graph with ggplot2
## Start building graphs
# x axis: years of experience, y-axis: salary
#plot 1
ggplot(data = Income, mapping = aes(x = Years, y = Salary))


#plot 2
#Add geoms_ in ggplot()
#add a scatter plot of Years and Salary
ggplot(data = Income, mapping = aes(x = Years, y = Salary)) + 
+   geom_point()

#plot 3
# using optional point color, transparency, and point size
ggplot(data = Income, mapping = aes(x = Years, y = Salary)) +
+   geom_point(color = "cornflowerblue", alpha = .7, size = 1.5) +
+   theme_bw()

#plot 4
# add a fitted line with linear regression
> ggplot(data = Income, 
+   mapping = aes(x = Years, y = Salary)) +
+   geom_point(color = "cornflowerblue", alpha = .7, size = 1.5) +
+   theme_bw() +
+   geom_smooth(method = "lm")  


#then we do some regression with another data frame
library(car)
#create a data frame states
states <- as.data.frame(state.x77[,c("Murder", "Population",
                      "Illiteracy", "Income", "Frost")])
#regression model
fit <- lm(Murder ~ Population + Illiteracy + Income + Frost, data=states)
 
# Cooks Distance D to test influential observation
# identify D values > 4/(n-k-1) 
cutoff <- 4/(nrow(states)-length(fit$coefficients)-2)
#to plot figure for checking influential observations.
plot(fit, which=4, cook.levels=cutoff)
abline(h=cutoff, lty=2, col="red")

#to show environment system in current session
parenvs(all=TRUE) 
   label                             name               
1  <environment: R_GlobalEnv>        ""                 
2  <environment: package:car>        "package:car"      
3  <environment: package:carData>    "package:carData"  
4  <environment: package:devtools>   "package:devtools" 
5  <environment: package:usethis>    "package:usethis"  
6  <environment: package:ggplot2>    "package:ggplot2"  
7  <environment: package:pryr>       "package:pryr"     
8  <environment: 0x0000023820ad0848> "tools:rstudio"    
9  <environment: package:stats>      "package:stats"    
10 <environment: package:graphics>   "package:graphics" 
11 <environment: package:grDevices>  "package:grDevices"
12 <environment: package:utils>      "package:utils"    
13 <environment: package:datasets>   "package:datasets" 
14 <environment: package:methods>    "package:methods"  
15 <environment: 0x000002381e3ee1f0> "Autoloads"        
16 <environment: base>               ""                 
17 <environment: R_EmptyEnv>         ""                 
> 

The environment system shows that the lowest environment is R_GlobalEnv, the global enrivonment, which is the current active environment. The highest environment R_EmptyEnv, the empty environment. And the parent environment of the R_GlobalEnv is package:car, and so on , one by one until to R_EmptyEnv.

Each environment can be referred to and shown its information by using as.environment() function. Following code shows the attribute and its path for environment ‘package:car’.

as.environment("package:car")
<environment: package:car>
attr(,"name")
[1] "package:car"
attr(,"path")
[1] "C:/Users/Wilso/AppData/Local/R/win-library/4.3/car"

To show objects connecting to an environment, you can use function ls(). The following code example show objects in global environment.

ls(globalenv())
[1] "cutoff"  "fit"     "Income"  "obj1"    "obj2"    "states" 
[7] "vartype"

environment() can be used to show the current active environment.

environment()
<environment: R_GlobalEnv>

R uses a mechanism called Scoping Rule to refer to object connected to its corresponding environment. The rule is when an object is referred to, R will first search this object from the current active environment, then search from its parent environment if it is not in the current active environment, and so on until the searching process reaches to the empty environment. A typical example is when R defines a function, a new environment specific for this function is created and become active in the session, and it is a child environment from the environment in which the function is created (e.g. the globalenv). And all the objects generated from inside the function definition block are connected to the function’s environment. The objects in the globalenv can also be referred to and used in the function block, because R will search an object in function environment’s parent if it is not generated from inside the function block. The next code example show this mechanism.

#to create a function in the current active environment, globalenv
aggre <- function(a,b) {a + b}
#to create another function
multi_aggre <- function (a,b,n) {aggre(a,b) * n}

#call the second function, and the first function can be referred to 
#from inside the second function
multi_aggre (1,2,3)
[1] 9

You can also watch videos on R programming on our YouTube channel.

wilsonzhang746

Recent Posts

Download R Course source files

Click here to download R Course source files !

2 months ago

Download Python Course source files

Click here to download Python Course Source Files !

2 months ago

How to create a data frame from nested dictionary with Pandas in Python

For online Python training registration, click here ! Pandas provides flexible ways of generating data…

5 months ago

How to delete columns of a data frame in Python

For online Python training registration, click here ! Data frame is the tabular data object…

5 months ago

Using isin() to check membership of a data frame in Python

Click her for course registration ! When a data frame in Python is created via…

5 months ago

How to assign values to Pandas data frame in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental…

5 months ago