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.
To see the packages that are available in the R session, use the
> search()  ".GlobalEnv" "package:graphics" "package:grDevices"  "package:utils" "package:datasets" "package:xts"  "package:zoo" "package:stats" "package:methods"  "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.
require if you want to add a package to the session. Quotes around the package name are optional:
> require(PortfolioProbe) Loading required package: PortfolioProbe
To see what packages are installed on you machine, use
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.
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:
The second is the R-forge repository:
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
install.packageswith the path to the file as the first argument and with the argument:
The following table shows the packages that are used in the Portfolio Probe Cookbook and the location of their main repository.
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.
- Remember that R is case-sensitive, including package names.
- If you are using an alternative repository, don’t forget the “http://” prefix for it.