R's functionality can be easily and significantly expanded through the use of packages. A package is a collection of functions that has been contributed by members of the R user community. Let us look at the steps involved in acquiring, installing, loading, and using a new package in R:
chooseCRANmirror()
command:> #acquiring, preparing, installing, and using a new R package > #step 1: choose a CRAN mirror > #open the CRAN mirror window using chooseCRANmirror() > #then choose the mirror located nearest to you > chooseCRANmirror()
install.packages()
command:> #step 2: install the package > #open the packages window using install.packages > #then choose a package to install it on your computer > install.packages()
Note that the appearance of the packages window may vary depending on the operating system and version that you use. Also note that R will automatically install any packages that the selected package depends upon to operate.
library(...)
function to load the a package for use in R.> #step 3: load the package > #use the library(...) function to load a package once it has been installed > #load the magic package > library(magic)
Note that R will automatically load any packages that the specified package depends upon to operate and notify you in the console. If no additional packages are necessary, R will drop down to the next line without providing any output.
magic(n)
function from the magic package to generate a sequence of magic squares:> #step 4: use the package > #once loaded, a package's functions can be used within the R console > #use the magic(n) function from the magic package to generate an 8x8 magic square > #n is a single nonnegative number that indicates how many rows and columns the magic square will have > #this function generates the same type of magic square that we encountered when solving Zhuge Liang's puzzle in chapter 3! > magic(8)
We expanded the capabilities of R by downloading, installing, and loading a package in the R console. Let us review the steps involved in this process.
The initial step in acquiring a new R package is to choose a CRAN mirror. The acronym CRAN stands for Comprehensive R Archive Network and refers to several worldwide servers that store and maintain R's code and documentation. A CRAN mirror is a single server in this network. When choosing a CRAN mirror, it is best to select the location nearest to you. Since the data that you request will travel a shorter distance, you will be able to download more content in less time. Using the chooseCRANmirror()
function will open the CRAN mirror window, which displays a list of all available CRAN mirrors.
Note that chooseCRANmirror()
only needs to be executed once each time that you launch R. Once a CRAN mirror is selected, it will remain active until you quit R.
Next, you will need to install the desired package. Executing the install.packages()
command in the R console will open the Packages window, which displays a list of all available packages.
In our example, we selected the magic
package. R then automatically installed the abind
package, which is required for magic
to function. Whenever necessary, R will automatically install required packages, known as dependencies, in this fashion.
A given package only needs to be installed once. It is then available to be loaded any time that you use R.
Also note that if you already know the name of the package, you can install it using a single install.packages(name)
command, such as install.packages("magic")
.
A list of every available R package, along with a description of each, can be found on the official R website at:
Then, to prepare the package for use in the R console, it must be loaded via the library(...)
function. This function receives an argument that indicates the name of the function. For instance, since we wanted to load the magic
package in our activity, the library(...)
function took on the following form:
> library(magic)
As with install.packages()
, the library(...)
command will automatically load any necessary dependencies. In our case, the abind
package was loaded after executing our library(...)
function. When no dependencies are present, R will drop down to the next line in the console without providing any output.
Once you have loaded a new package in R, the final step is to take advantage of its offerings. Quite simply, once a package has been loaded, you can use any of its functions, just as we have been using R's built-in functions throughout this book.
In our activity, we loaded the magic package, which gave us access to several functions related to magic squares. We used the magic(n)
function to generate an 8x8 magic square.
This is the same variety of magic square that we encountered when solving Zhuge Liang's puzzle in Chapter 3. In fact, the puzzle that you solved in that chapter was generated using R and the magic
package!
All R packages can be installed by following this same procedure. The immense value of R packages is that they expand the capabilities of R. Thousands of packages are currently available and new ones are continuously being created by the R user community. This means that R is perpetually growing in scope and functionality.
chooseCRANmirror()
function be executed in R?a. Once.
b. Once each time R is launched.
c. Once each time a given package is installed.
d. Once each time a given package is loaded.
install.packages()
function be executed in R?a. Once.
b. Once each time R is launched.
c. Once each time a given package is installed.
d. Once each time a given package is loaded.
library(...)
function be executed in R?a. Once.
b. Once each time R is launched.
c. Once each time a given package is installed.
d. Once each time a given package is loaded.
Use the R website's online package listing (http://cran.r-project.org/web/packages), or one of the other resources presented in this chapter, to learn about the packages that are available in R. Choose one that will be useful to your work. Then install it in R and experiment with its functions.