After successfully installing R, users can download, install, and update packages from the repositories. As R allows users to create their own packages, official and non-official repositories are provided to manage these user-created packages. CRAN is the official R package repository. Currently, the CRAN package repository features 11,589 available packages (as of 10/11/2017). Through the use of the packages provided on CRAN, users may extend the functionality of R to machine learning, statistics, and related purposes. CRAN is a network of FTP and web servers around the world that store identical, up-to-date versions of code and documentation for R. You may select the closest CRAN mirror to your location to download packages.
Perform the following steps to install and load R packages:
- Load a list of installed packages:
> library()
- Set the default CRAN mirror:
> chooseCRANmirror()
R will return a list of CRAN mirrors, and then ask the user to either type a mirror ID to select it, or enter zero to exit:
- Install a package from CRAN; take package
e1071
as an example:
> install.packages("e1071")
- Update a package from CRAN; take package
e1071
as an example:
> update.packages("e1071")
- Load the package:
> library(e1071)
- If you would like to view the documentation of the package, you can use the
help
function:
> help(package ="e1071")
- If you would like to view the documentation of the function, you can use the
help
function:
> help(svm, e1071)
- Alternatively, you can use the help shortcut,
?
, to view the help document for this function:
> ?e1071::svm
- If the function does not provide any documentation, you may want to search the supplied documentation for a given keyword. For example, if you wish to search for documentation related to
svm
:
> help.search("svm")
- Alternatively, you can use
??
as the shortcut forhelp.search
:
> ??svm
- To view the argument taken for the function, simply use the
args
function. For example, if you would like to know the argument taken for thelm
function:
> args(lm)
- Some packages will provide examples and demos; you can use
example
ordemo
to view an example or demo. For example, one can view an example of thelm
package and a demo of thegraphics
package by typing the following commands:
> example(lm)> demo(graphics)
- To view all the available demos, you may use the
demo
function to list all of them:
> demo()
This recipe first introduces how to view loaded packages, install packages from CRAN, and load new packages. Before installing packages, those of you who are interested in the listing of the CRAN package can refer to http://cran.r-project.org/web/packages/available_packages_by_name.html.
When a package is installed, documentation related to the package is also provided. You are, therefore, able to view the documentation or the related help pages of installed packages and functions. Additionally, demos and examples are provided by packages that can help users understand the capability of the installed package.
- Besides installing packages from CRAN, there are other R package repositories, including Crantastic, a community site for rating and reviewing CRAN packages, and R-Forge, a central platform for the collaborative development of R packages. In addition to this, Bioconductor provides R packages for the analysis of genomic data.
- If you would like to find relevant functions and packages, please visit the list of task views at http://cran.r-project.org/web/views/, or search for keywords at http://rseek.org.