The vast majority of analysis procedures available in R are located in special-purpose packages. You can think of these as a group of functions that you load into R and then can use along with your data. For example, diagnostics for regression are located in the cars package, for multiple comparisons in the multcomp package, and so on. In order to use a package, it must first be installed, and then you can load it into the specific R Code as needed.
Technical details
Installing packages
If you wish to use a function in a particular function, it 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 in the software. You can also see a full list of the packages and their versions by creating a new Calculation with the code installed.packages(). You can also test loading the package you need to see if it is installed using the code, library(packagename). If you need to use a package that is not installed, please contact support.
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 a Calculation, or,require(packageName)in a line above the line where you first use a function. For example:library(flipMultariates) my.lda = LDA(x, y) - Type
packageName::functionName. For example,flipMultivariates::LDA(x, y), whereLDAis a function in theflipMultivariatespackage. 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 theLDAobject).
Automatic loading of required packages
Once an R Output has been created using a particular package, 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 packages. 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
R prefers more recently loaded packages to packages loaded earlier. So 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. The software organizes that the automatically loaded packages are loaded before the explicitly loaded ones in the code, so the implicit ones 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.