R Packages and Q

From Q
Jump to navigation Jump to search

The vast majority of analyses procedures available in R are located in special-purpose packages. For example, diagnostics for regression are located in the cars package, for multiple comparisons in the multcomp package, and so on. There are in excess of 10,000 different packages.

This page describes how to install and load packages. When using Standard R this is all done automatically, and you do not need to know anything about packages.

Installing packages

If you wish to use a function in a particular function, it is is necessary to first install the package that contains the function. Many of the most popular packages are already installed in the R Servers that are used to perform analysis for Q. The list of current packages is:

You can also see which packages have been installed, and their versions, by creating a new R Output with code of installed.packages().

If you wish to use a package that is not installed, please contact support. Provided that there are no technical problems with the package, it will usually take a day or two to install.

Loading packages

Once a package has been installed, the next step is to load the package. There are a number of ways of doing this. The most common are:

  • Type library(packageName) in an R Output, or, require(packageName), in a line above the line where you first use a function. For example:
  • Type packageName::functionName. For example, flipMultivariates::LDA(x, y), where LDA is a function in the flipMultivariates package. This approach is inadvisable when creating a class that is specific to the package, as it only loads the package to perform the specified function, so other functions and methods that are in the package will not be available (e.g., predict.LDA) will not be available automatically if :: has been used to create the LDA object).
library(flipMultariates)
my.lda = LDA(x, y)

Automatic loading of required packages

Once an R Output has been created using a particular package, that when this R Output is used in other R Outputs, the package used to create it will automatically be loaded. As an example, consider two R items:

  library(mgcv)
  mydata = read.csv("http://www.ats.ucla.edu/stat/data/binary.csv")
  mylogit_2 = gam(admit ~ s(gre) + gpa + rank, data = mydata, family = "binomial")

and

  summary(mylogit_2)

Note that the second R item (a) refers to mylogit_2, and (b) does not explicitly load any pacakges. As the second R item refers to the first, library(mgcv) is implicitly loaded when summary(mylogit_2) is updated.

Controlling the Ordering of the Loading of Packages

There is one small caveat to the statement that a dependent R item need not include library() or require() statements which will be implicitly included. This caveat arises because a function or some other object (e.g., data) may have the same name in multiple packages. In this case, R prefers more recently loaded packages to packages loaded earlier. Q organizes that the automatically loaded packages are loaded before the explicitly loaded ones, so the implicit have lower preference when R resolves conflicting names. It may be the case that the order of packages does not suit your use: the solution is simple - just add an additional library statement to give preference to that package. One can see the order in which the packages are loaded by looking at the raw R output display as described above.