Using R packages

Explanation

Packages are a basic unit of functionality in R.  A package typically contains a number of functions pertinent to a topic.  Some packages contain data objects (in addition to or instead of functions).

There is a distinction between packages that are installed on your machine and packages that are loaded into a particular R session.

Doing it

Session packages

To see the packages that are available in the R session, use the search function:

> search()
 [1] ".GlobalEnv"        "package:graphics"  "package:grDevices"
 [4] "package:utils"     "package:datasets"  "package:xts"      
 [7] "package:zoo"       "package:stats"     "package:methods"  
[10] "Autoloads"         "package:base"

The first item on the search list is your global environment.  The rest (except for the weirdo Autoloads) are the packages that are currently loaded in the session.  (In this case, that is — it is possible to attach files created by save and some data objects so that they appear on the search list.)

These are the packages that you can currently use in the session.

Use require if you want to add a package to the session.  Quotes around the package name are optional:

> require(PortfolioProbe)
Loading required package: PortfolioProbe

Installed packages

To see what packages are installed on you machine, use library:

> library()

This shows the packages that are in the default library, there may be additional libraries on the machine — see the help for library for more on this.

Installing packages

You install packages into the default library with install.packages.  For example:

> install.packages("fBasics")
Installing package(s) into ‘C:/Users/pat/Documents/R/win-library/2.15’
(as ‘lib’ is unspecified)
--- Please select a CRAN mirror for use in this session ---
trying URL 'http://cran.ma.imperial.ac.uk/bin/windows/contrib/2.15/fBasics_2160.83.zip'
Content type 'application/zip' length 1374231 bytes (1.3 Mb)
opened URL
downloaded 1.3 Mb

package ‘fBasics’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
        C:\Users\pat\AppData\Local\Temp\Rtmpqmydgm\downloaded_packages

Package names do need to be quoted.  This is using the default repository, which is CRAN.

There are two other repositories of interest.  The first is the Portfolio Probe repository:

install.packages("pprobeData", 
   repos="http://www.portfolioprobe.com/R")

The second is the R-forge repository:

install.packages("PortfolioAnalytics", 
    repos="http://r-forge.r-project.org")

firewall

The installing we have done so far has assumed that R has access to the internet.  If there are firewall issues with that, then the alternative is:

  • Download the package using a browser that does have access to the internet
  • Save the package somewhere on your file system
  • Use install.packages with the path to the file as the first argument and with the argument: repos=NULL

Specific packages

The following table shows the packages that are used in the Portfolio Probe Cookbook and the location of their main repository.

package repository
"BurStFin" CRAN
"PortfolioProbe" Portfolio Probe
"pprobeData" Portfolio Probe
"TTR" CRAN

Updating packages

You can update a package with install.packages just like installing it the first time.  But the package can not be loaded in the session when you try to re-install it.

Troubleshooting

  • Remember that R is case-sensitive, including package names. "PortfolioProbe" and "Portfolioprobe" are different.
  • If you are using an alternative repository, don’t forget the “http://” prefix for it.

See also

Some hints for the R beginner.

Navigate