Package management in R

Hi,

We have seen how to load the data into R language in my previous post Loading Data into R. It is an important part of this blog series. Let’s talk about packages now.

Packages are not new to programmers. Any programming language comes with packages, of course limited set of packages. Additional packages are added a la carte. We shall see same behavior in R as well. The default installation of is a thin solution, which has only basic packages. If needed we need to add additional packages. Lets see how.

Viewing the packages

search() would help us to check the list of loaded packages.

> search()
 [1] ".GlobalEnv"        "package:readr"     "tools:rstudio"     "package:stats"    
 [5] "package:graphics"  "package:grDevices" "package:utils"     "package:datasets" 
 [9] "package:methods"   "Autoloads"         "package:base"

installed.packages() shows us the packages installed but not loaded.

> installed.packages()
             Package        LibPath                                   Version   
BH           "BH"           "D:/gandhari/documents/R/win-library/3.4" "1.65.0-1"
hms          "hms"          "D:/gandhari/documents/R/win-library/3.4" "0.3"     
R6           "R6"           "D:/gandhari/documents/R/win-library/3.4" "2.2.2"   
Rcpp         "Rcpp"         "D:/gandhari/documents/R/win-library/3.4" "0.12.12" 
readr        "readr"        "D:/gandhari/documents/R/win-library/3.4" "1.1.1"   
rlang        "rlang"        "D:/gandhari/documents/R/win-library/3.4" "0.1.2"   
tibble       "tibble"       "D:/gandhari/documents/R/win-library/3.4" "1.3.4"   
base         "base"         "C:/Program Files/R/R-3.4.1/library"      "3.4.1"   
boot         "boot"         "C:/Program Files/R/R-3.4.1/library"      "1.3-19"  
class        "class"        "C:/Program Files/R/R-3.4.1/library"      "7.3-14"  
cluster      "cluster"      "C:/Program Files/R/R-3.4.1/library"      "2.0.6"   
codetools    "codetools"    "C:/Program Files/R/R-3.4.1/library"      "0.2-15"  
compiler     "compiler"     "C:/Program Files/R/R-3.4.1/library"      "3.4.1"   
datasets     "datasets"     "C:/Program Files/R/R-3.4.1/library"      "3.4.1"   
foreign      "foreign"      "C:/Program Files/R/R-3.4.1/library"      "0.8-69"  
graphics     "graphics"     "C:/Program Files/R/R-3.4.1/library"      "3.4.1"   
grDevices    "grDevices"    "C:/Program Files/R/R-3.4.1/library"      "3.4.1"   
grid         "grid"         "C:/Program Files/R/R-3.4.1/library"      "3.4.1"   
KernSmooth   "KernSmooth"   "C:/Program Files/R/R-3.4.1/library"      "2.23-15" 
lattice      "lattice"      "C:/Program Files/R/R-3.4.1/library"      "0.20-35" 
MASS         "MASS"         "C:/Program Files/R/R-3.4.1/library"      "7.3-47"  
Matrix       "Matrix"       "C:/Program Files/R/R-3.4.1/library"      "1.2-10"  
methods      "methods"      "C:/Program Files/R/R-3.4.1/library"      "3.4.1"   
mgcv         "mgcv"         "C:/Program Files/R/R-3.4.1/library"      "1.8-17"  
nlme         "nlme"         "C:/Program Files/R/R-3.4.1/library"      "3.1-131" 
nnet         "nnet"         "C:/Program Files/R/R-3.4.1/library"      "7.3-12"  
parallel     "parallel"     "C:/Program Files/R/R-3.4.1/library"      "3.4.1"   
rpart        "rpart"        "C:/Program Files/R/R-3.4.1/library"      "4.1-11"  
spatial      "spatial"      "C:/Program Files/R/R-3.4.1/library"      "7.3-11"  
splines      "splines"      "C:/Program Files/R/R-3.4.1/library"      "3.4.1"   
stats        "stats"        "C:/Program Files/R/R-3.4.1/library"      "3.4.1"   
stats4       "stats4"       "C:/Program Files/R/R-3.4.1/library"      "3.4.1"   
survival     "survival"     "C:/Program Files/R/R-3.4.1/library"      "2.41-3"  
tcltk        "tcltk"        "C:/Program Files/R/R-3.4.1/library"      "3.4.1"   
tools        "tools"        "C:/Program Files/R/R-3.4.1/library"      "3.4.1"   
translations "translations" "C:/Program Files/R/R-3.4.1/library"      "3.4.1"   
utils        "utils"        "C:/Program Files/R/R-3.4.1/library"      "3.4.1"   
             Priority      Depends                                          
BH           NA            NA                                               
hms          NA            NA                                               
R6           NA            "R (>= 3.0)"                                     
Rcpp         NA            "R (>= 3.0.0)"                                   
readr        NA            "R (>= 3.0.2)"                                   
rlang        NA            "R (>= 3.1.0)"                                   
tibble       NA            "R (>= 3.1.0)"                                   
base         "base"        NA                                               
boot         "recommended" "R (>= 3.0.0), graphics, stats"                  
class        "recommended" "R (>= 3.0.0), stats, utils"                     
cluster      "recommended" "R (>= 3.0.1)"                                   
codetools    "recommended" "R (>= 2.1)"                                     
compiler     "base"        NA                                               
datasets     "base"        NA                                               
foreign      "recommended" "R (>= 3.0.0)"                                   
graphics     "base"        NA                                               
grDevices    "base"        NA                                               
grid         "base"        NA                                               
KernSmooth   "recommended" "R (>= 2.5.0), stats"                            
lattice      "recommended" "R (>= 3.0.0)"                                   
MASS         "recommended" "R (>= 3.1.0), grDevices, graphics, stats, utils"
Matrix       "recommended" "R (>= 3.0.1)"                                   
methods      "base"        NA                                               
mgcv         "recommended" "R (>= 2.14.0), nlme (>= 3.1-64)"                
nlme         "recommended" "R (>= 3.0.2)"                                   
nnet         "recommended" "R (>= 2.14.0), stats, utils"                    
parallel     "base"        NA                                               
rpart        "recommended" "R (>= 2.15.0), graphics, stats, grDevices"      
spatial      "recommended" "R (>= 3.0.0), graphics, stats, utils"           
splines      "base"        NA                                               
stats        "base"        NA                                               
stats4       "base"        NA                                               
survival     "recommended" "R (>= 2.13.0)"                                  
tcltk        "base"        NA                                               
tools        "base"        NA                                               
translations NA            NA                                               
utils        "base"        NA                                               
             Imports                                            LinkingTo 
BH           NA                                                 NA        
hms          "methods"                                          NA        
R6           NA                                                 NA        
Rcpp         "methods, utils"                                   NA        
readr        "Rcpp (>= 0.12.0.5), tibble, hms, R6"              "Rcpp, BH"
rlang        NA                                                 NA        
tibble       "methods, rlang, Rcpp (>= 0.12.3), utils"          "Rcpp"    
base         NA                                                 NA        
boot         NA                                                 NA        
class        "MASS"                                             NA        
cluster      "graphics, grDevices, stats, utils"                NA        
codetools    NA                                                 NA        
compiler     NA                                                 NA        
datasets     NA                                                 NA        
foreign      "methods, utils, stats"                            NA        
graphics     "grDevices"                                        NA        
grDevices    NA                                                 NA        
grid         "grDevices, utils"                                 NA        
KernSmooth   NA                                                 NA        
lattice      "grid, grDevices, graphics, stats, utils"          NA        
MASS         "methods"                                          NA        
Matrix       "methods, graphics, grid, stats, utils, lattice"   NA        
methods      "utils, stats"                                     NA        
mgcv         "methods, stats, graphics, Matrix"                 NA        
nlme         "graphics, stats, utils, lattice"                  NA        
nnet         NA                                                 NA        
parallel     "tools, compiler"                                  NA        
rpart        NA                                                 NA        
spatial      NA                                                 NA        
splines      "graphics, stats"                                  NA        
stats        "utils, grDevices, graphics"                       NA        
stats4       "graphics, methods, stats"                         NA        
survival     "graphics, Matrix, methods, splines, stats, utils" NA        
tcltk        "utils"                                            NA        
tools        NA                                                 NA        
translations NA                                                 NA        
utils        NA                                                 NA        
             Suggests                                                                                   
BH           NA                                                                                         
hms          "testthat, lubridate"                                                                      
R6           "knitr, microbenchmark, pryr, testthat, ggplot2, scales"                                   
Rcpp         "RUnit, inline, rbenchmark, highlight, pkgKitten (>= 0.1.2)"                               
readr        "curl, testthat, knitr, rmarkdown, stringi, covr"                                          
rlang        "knitr, rmarkdown (>= 0.2.65), testthat, covr"                                             
tibble       "covr, dplyr, knitr (>= 1.5.32), microbenchmark, nycflights13,\ntestthat, rmarkdown, withr"
base         "methods"                                                                                  
boot         "MASS, survival"                                                                           
class        NA                                                                                         
cluster      "MASS"                                                                                     
codetools    NA                                                                                         
compiler     NA                                                                                         
datasets     NA                                                                                         
foreign      NA                                                                                         
graphics     NA                                                                                         
grDevices    "KernSmooth"                                                                               
grid         "lattice"                                                                                  
KernSmooth   "MASS"                                                                                     
lattice      "KernSmooth, MASS, latticeExtra"                                                           
MASS         "lattice, nlme, nnet, survival"                                                            
Matrix       "expm, MASS"                                                                               
methods      "codetools"                                                                                
mgcv         "splines, parallel, survival, MASS"                                                        
nlme         "Hmisc, MASS"                                                                              
nnet         "MASS"                                                                                     
parallel     "methods"                                                                                  
rpart        "survival"                                                                                 
spatial      "MASS"                                                                                     
splines      "Matrix, methods"                                                                          
stats        "MASS, Matrix, SuppDists, methods, stats4"                                                 
stats4       NA                                                                                         
survival     NA                                                                                         
tcltk        NA                                                                                         
tools        "codetools, methods, xml2, curl"                                                           
translations NA                                                                                         
utils        "methods, XML"                                                                             
             Enhances                                License                    
BH           NA                                      "BSL-1.0"                  
hms          NA                                      "GPL-3"                    
R6           NA                                      "MIT + file LICENSE"       
Rcpp         NA                                      "GPL (>= 2)"               
readr        NA                                      "GPL (>= 2) | file LICENSE"
rlang        NA                                      "GPL-3"                    
tibble       NA                                      "MIT + file LICENSE"       
base         NA                                      "Part of R 3.4.1"          
boot         NA                                      "Unlimited"                
class        NA                                      "GPL-2 | GPL-3"            
cluster      NA                                      "GPL (>= 2)"               
codetools    NA                                      "GPL"                      
compiler     NA                                      "Part of R 3.4.1"          
datasets     NA                                      "Part of R 3.4.1"          
foreign      NA                                      "GPL (>= 2)"               
graphics     NA                                      "Part of R 3.4.1"          
grDevices    NA                                      "Part of R 3.4.1"          
grid         NA                                      "Part of R 3.4.1"          
KernSmooth   NA                                      "Unlimited"                
lattice      "chron"                                 "GPL (>= 2)"               
MASS         NA                                      "GPL-2 | GPL-3"            
Matrix       "MatrixModels, graph, SparseM, sfsmisc" "GPL (>= 2) | file LICENCE"
methods      NA                                      "Part of R 3.4.1"          
mgcv         NA                                      "GPL (>= 2)"               
nlme         NA                                      "GPL (>= 2) | file LICENCE"
nnet         NA                                      "GPL-2 | GPL-3"            
parallel     "snow, nws, Rmpi"                       "Part of R 3.4.1"          
rpart        NA                                      "GPL-2 | GPL-3"            
spatial      NA                                      "GPL-2 | GPL-3"            
splines      NA                                      "Part of R 3.4.1"          
stats        NA                                      "Part of R 3.4.1"          
stats4       NA                                      "Part of R 3.4.1"          
survival     NA                                      "LGPL (>= 2)"              
tcltk        NA                                      "Part of R 3.4.1"          
tools        NA                                      "Part of R 3.4.1"          
translations NA                                      "Part of R 3.4.1"          
utils        NA                                      "Part of R 3.4.1"          
             License_is_FOSS License_restricts_use OS_type MD5sum NeedsCompilation
BH           NA              NA                    NA      NA     "no"            
hms          NA              NA                    NA      NA     "no"            
R6           NA              NA                    NA      NA     "no"            
Rcpp         NA              NA                    NA      NA     "yes"           
readr        NA              NA                    NA      NA     "yes"           
rlang        NA              NA                    NA      NA     "yes"           
tibble       NA              NA                    NA      NA     "yes"           
base         NA              NA                    NA      NA     NA              
boot         NA              NA                    NA      NA     "no"            
class        NA              NA                    NA      NA     "yes"           
cluster      NA              NA                    NA      NA     "yes"           
codetools    NA              NA                    NA      NA     "no"            
compiler     NA              NA                    NA      NA     NA              
datasets     NA              NA                    NA      NA     NA              
foreign      NA              NA                    NA      NA     "yes"           
graphics     NA              NA                    NA      NA     "yes"           
grDevices    NA              NA                    NA      NA     "yes"           
grid         NA              NA                    NA      NA     "yes"           
KernSmooth   NA              NA                    NA      NA     "yes"           
lattice      NA              NA                    NA      NA     "yes"           
MASS         NA              NA                    NA      NA     "yes"           
Matrix       NA              NA                    NA      NA     "yes"           
methods      NA              NA                    NA      NA     "yes"           
mgcv         NA              NA                    NA      NA     "yes"           
nlme         NA              NA                    NA      NA     "yes"           
nnet         NA              NA                    NA      NA     "yes"           
parallel     NA              NA                    NA      NA     "yes"           
rpart        NA              NA                    NA      NA     "yes"           
spatial      NA              NA                    NA      NA     "yes"           
splines      NA              NA                    NA      NA     "yes"           
stats        NA              NA                    NA      NA     "yes"           
stats4       NA              NA                    NA      NA     NA              
survival     NA              NA                    NA      NA     "yes"           
tcltk        NA              NA                    NA      NA     "yes"           
tools        NA              NA                    NA      NA     "yes"           
translations NA              NA                    NA      NA     NA              
utils        NA              NA                    NA      NA     "yes"           
             Built  
BH           "3.4.1"
hms          "3.4.1"
R6           "3.4.1"
Rcpp         "3.4.1"
readr        "3.4.1"
rlang        "3.4.1"
tibble       "3.4.1"
base         "3.4.1"
boot         "3.4.1"
class        "3.4.1"
cluster      "3.4.1"
codetools    "3.4.1"
compiler     "3.4.1"
datasets     "3.4.1"
foreign      "3.4.1"
graphics     "3.4.1"
grDevices    "3.4.1"
grid         "3.4.1"
KernSmooth   "3.4.1"
lattice      "3.4.1"
MASS         "3.4.1"
Matrix       "3.4.1"
methods      "3.4.1"
mgcv         "3.4.1"
nlme         "3.4.1"
nnet         "3.4.1"
parallel     "3.4.1"
rpart        "3.4.1"
spatial      "3.4.1"
splines      "3.4.1"
stats        "3.4.1"
stats4       "3.4.1"
survival     "3.4.1"
tcltk        "3.4.1"
tools        "3.4.1"
translations "3.4.1"
utils        "3.4.1"

R Studio IDE has a tab which shows the loaded/not loaded packages.

R studio 10 - packages tab

Installing new packages

To install a new package we shall use Install packages option in R Studio, or install.packages() command.

R studio 11 - installing new packages

> install.packages("regress")
Installing package into ‘D:/gandhari/documents/R/win-library/3.4’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/regress_1.3-15.zip'
Content type 'application/zip' length 32695 bytes (31 KB)
downloaded 31 KB

package ‘regress’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
	C:\Users\pandian\AppData\Local\Temp\Rtmpq29bYI\downloaded_packages

Removing packages

Removing a package using R Studio is as easy as clicking the x mark.

R studio 12 - removing packages.PNG

To do it from console, we shall use remove.packages()

> remove.packages("regress")
Removing package from ‘D:/gandhari/documents/R/win-library/3.4’
(as ‘lib’ is unspecified)

Loading the packages

To load a package, we shall just check ✅ the needed package in package tab. The same task shall be performed using library() command in console.

R studio 13 - loading the package

> library("BH", lib.loc="~/R/win-library/3.4")

See in another interesting post. 💖 you all.

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s