access_time 11 months ago languageEnglish
more_vert

Working with R Packages and IDE

visibility 87 comment 0

In this series, we've walked-through R programming basics and advanced data types. This article will focus on R packages and IDEs so that you can program efficiently with R.

R terminologies

Let's recap these commonly mentioned R terminologies:

  • Package: An extension of the R base system with code, data and documentation in standardized format.
  • Library: A directory containing installed packages.
  • Repository: A website providing packages for installation.
  • Source: The original version of a package with human-readable text and code.
  • Binary: A compiled version of a package with computer-readable text and code, may work only on a specific platform.
  • Base packages: Part of the R source tree, maintained by R Core team.
  • Recommended packages: Part of every R installation, but not necessarily maintained by R Core.
  • Contributed packages: All the rest.

R library & packages

There are two types of libraries in a R environment:

  • System library: Contains built in packages distributed with R client
  • User library: Contains packages manually installed via CRAN or manual download 

In RStudio, you can choose different R system library via Tools -> Global Options.

As shown above, I have multiple R distributions in my computer that can be used. 
For R packages, we can search, install and load them easily using system functions:
  • List all the packages currently loaded: 
search()
  • List all installed packages
installed.packages()
  • Install packages from CRAN (Comprehensive R Archiving Network)
install.packages("PackageName")
  • Install packages manually
install.packages("local package path", repos=NULL, type="source")
  • Remove packages
remove.packages()
  • Load packages
library(“PackageName”)
require(PackageName)

The following are some examples (script R17.Library.R)

Get library path

Run the following function to get current library path in the project:

# Get library path, it can returns different results in different IDEs based on the settings
.libPaths()

[1] "E:/Documents/R/win-library/3.4"                      
[2] "C:/Program Files/Microsoft/R Client/R_SERVER/library"

List available packages

# Get list of all the packages installed in all library
library()

In RStudio, it will open a window that list all the packages in each library:

Install package from CRAN

The following code snippet installs XML package 

# install package from CRAN
install.packages("XML")

Show currently loaded package

> # currently loaded packages
> search()
 [1] ".GlobalEnv"            "tools:rstudio"         "package:RevoUtilsMath"
 [4] "package:RevoUtils"     "package:RevoMods"      "package:MicrosoftML"  
 [7] "package:mrsdeploy"     "package:RevoScaleR"    "package:lattice"      
[10] "package:rpart"         "package:stats"         "package:graphics"     
[13] "package:grDevices"     "package:utils"         "package:datasets"     
[16] "package:methods"       "Autoloads"             "package:base"       

Load library

Function library() can be used to load library. 

> # load library
> library("XML")
> 
> # Run search again
> search()
 [1] ".GlobalEnv"            "package:XML"           "tools:rstudio"        
 [4] "package:RevoUtilsMath" "package:RevoUtils"     "package:RevoMods"     
 [7] "package:MicrosoftML"   "package:mrsdeploy"     "package:RevoScaleR"   
[10] "package:lattice"       "package:rpart"         "package:stats"        
[13] "package:graphics"      "package:grDevices"     "package:utils"        
[16] "package:datasets"      "package:methods"       "Autoloads"            
[19] "package:base"  

The above search function shows XML package is loaded. We can also use programming to check whether a package is loaded:

> # Check whether package is currently loaded.
> if("package:XML" %in% search())
+ {
+   print("XML package is currently loaded")
+ }else
+ {
+   print("XML package is not currently loaded")
+ }
[1] "XML package is currently loaded"

Working directory

In R, we can get current working directory using the following function:

getwd()

Working directory can also be set dynamically using setwd function:

setwd(dir)

To list files in current directory, use list.files():

list.files()

The following is the working directory for this R programming tutorial in my computer:

Examples

The following are the examples about working directory (script R18.WorkingDirectory.R):

# get current directory
getwd()

# Set working directory
setwd("F:/Projects/RTutorialSamples")

# list files in this directory

list.files(all.files = TRUE)

.RData

R objects and variables can be saved to .RData file.

To save list of object, use function save()To save variables in current working space using save.image() or save function:

save(list = ls(all.names = TRUE), file = ".RData", envir = .GlobalEnv)

Saved objects can be reloaded through load() function.

Here are some examples about .RData (script R19.RDataAndRHistory.R):

# Save objects into data files
x<- 1:10
y <- list(a = 1, b = TRUE, c = "chars")
save(x, y, file = "xy.RData")

# save my currenct working sapce
save.image()
# or use save commands
save(list = ls(all.names = TRUE), file = ".RData", envir = .GlobalEnv)

# load file
load("xy.RData")

# delete file
unlink("xy.RData")

.RHistory

.RHistory in working directory stores all the historical commands that have been used previously. 

In RStudio, the history commands are displayed in the History tab:

Useful tips

In RStudio, there are few useful tips that are commonly used: 

Clear console

  • Run function: cat("\014") 
  • Or press CTRL + L

Function help

Use ? to get help

?save 

The output looks like the following:

Search keyword

You can also search certain keyword in the help documentations:

help.search("keyword")

Examples

Here are some examples about getting help information (script R20.Helps.R):

?help

?save

help.search("XML")

help.search("lm")

Summary

Hopefully, you now have a good idea about how to use R IDEs efficiently and how to install and load external packages.

copyright The content on this page is licensed under CC-BY-SA-4.0.
Like this article?
Share on

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

Follow Kontext

Get our latest updates on LinkedIn.

Want to contribute on Kontext to help others?

Learn more

More from Kontext

Plotting with R (Part I)
visibility 54
thumb_up 0
access_time 11 months ago
visibility 43
thumb_up 0
access_time 11 months ago
visibility 58
thumb_up 0
access_time 11 months ago