# R Programming/Packages

R Programming
* R Basics
** Introduction
** Sample Session
** Manage your workspace
** Settings and Installation
** Documentation
** Control Structures
** Working with functions
** Debugging
** Using C or Fortran
** Utilities
** Estimation utilities
** Packages
* Data Management
** Data types
** Working with data frames
** Importing and exporting data
** Text Processing
** Times and Dates
* Graphics
** Grammar of graphics
* Publication quality output
* Descriptive Statistics
* Mathematics
** Optimization
** Probability Distributions
** Random Number Generation
* Statistical Core Methods
** Maximum Likelihood
** Method of Moments
** Bayesian Methods
** Bootstrap
** Multiple Imputation
** Nonparametric Methods
* Regression Models
** Linear Models
** Quantile Regression
** Binomial Models
** Multinomial Models
** Tobit And Selection Models
** Count Data Models
** Duration Analysis
* Time Series
* Factor Analysis
* Classification
** Ordination
** Clustering
* Network Analysis
* High Performance Computing
** Profiling R code
** Parallel computing with R
* Appendix
** Sources
** Index
An R package includes a set of functions and datasets. Packages are often developed as supplementary material to books. For instance the MASS package was developed by Venables and Ripley for their book Modern Applied Statistics with S and the car package was developed by John Fox for his book An R and S plus Companion to Applied Regression.

## Load a package

A package is loaded into the current R environment using the library function. A list of functions and datasets included in a package can be obtained by using the h or help argument of the library function.
A package can be detached from the current environment by using the detach function:
Without any arguments the library function lists all of the packages currently available to the user. env (gdata) describe all loaded environments (ie packages). search gives the list of all loaded packages.
current.packages (Zelig) show all the required and suggested packages.
Where are my packages stored?
* The .libPaths function without arguments prints the library directories
* The .libPaths function with a directory as argument defines a new directory where to store new libraries.

## Install new packages

* Each major distribution of R includes a 'base' set of packages which support many basic statistical functions.
* Many R Users also choose to install additional 'Add-on' packages to provide simplified interfaces to R commands or to add specialist functionality i.e. the ggplot Grammar of Graphics package provides an advanced graphical output capability.
* The exhaustive list of all available packages is on the CRAN website.
* The R community has developed a vast resource of Add-on packages, some with unique functionality, some with overlapping functionality. It is therefore common to find multiple R packages capable of completing the same task i.e. reading and writing Excel spreadsheets. Ultimately which package to use is your choice.
* To install a new package, it is usually necessary to specify the name of the package as an argument of install.packages function.
* Sometimes you need to specify more options. For instance, this is the case if you are not an administrator of your computer.
** "lib" specifies the directory where you want to store the package.
** "repos" specifies a list of repositories. Note that you can specify a vector of repositories.
** "dep=T" specifies that all the required packages are also downloaded and installed.
* Stay up to date.
If you want to be aware of the latest packages, type new.packages in R or visit the Revolution Computing Blog which gives each month a list of the new and the updated packages.
We can also install bundles of packages using install.views or update.views (ctv).
We can also remove packages with remove.packages.

## Package Documentation and Help

All R packages install with 'help' documentation, listing their functions and providing syntax and usage examples.
See the Obtaining Help Documentation section for more details on accessing package 'help' documentation.

## Package Dependencies

* Most R packages have dependencies or references to other R packages. You must have all of an R package's 'required' dependencies installed, before you can use the package.
* R package dependencies come in two types, required and suggested.
* Specialist R packages such as the ggplot Grammar of Graphics packages have large package dependency trees.
* The install.packages function will automatically download and install a package and its dependencies, on a computer with an Internet connection.
* The R CMD INSTALL utility will check preinstalled packages for dependencies, but not download missing packages.
* Users must follow separate package download and installation processes when working on a computer with no Internet connection. The miniCRAN package can be used to assist in the offline management of R package dependencies.

## Building R Packages

You can write down your own R packages. But, all packages submitted to CRAN (or Bioconductor) must follow specific guidelines, including the folder structure of the package and the other files like DESCRIPTION, NAMESPACE and so on.
* See Friedrich Leisch's introduction (PDF 20 pages)
* See also Duncan Murdoch's tools for building packages using Windows
* See also Hadley Wickham and Jennifer Bryan's online book on current packaging practices (R Packages)

## References

* ↑ Friedrich Leisch Creating R Packages : A Tutorial http://cran.r-project.org/doc/contrib/Leisch-CreatingPackages.pdf
* ↑ http://www.r-project.org/conferences/useR-2008/slides/Murdoch.pdf
* ↑ Hadley Wickham and Jennifer Bryan R Packages : Organize, Test, Docment and Share your code https://r-pkgs.org/