Downloading a Package from the Web

The package you want may not be in your R installation. One of the big advantages of open source software is that people love to share. People all over the world have written their own special-purpose R packages, placing them in the CRAN repository and elsewhere.

Note

User contributions to CRAN go through a vetting process and are generally of high quality. They are, however, not tested as throughly as R itself.

One way to install a package is to use the install_packages() function. For example, suppose you wish to use the mvtnorm package, which computes multivariate normal cumulative distribution functions and other quantities. First, choose a directory in which you wish to install the package (and maybe others in the future), say /a/b/c. Then at the R prompt, type this:

> install.packages("mvtnorm","/a/b/c/")

This will cause R to automatically go to CRAN, download the package, compile it, and load it into a new directory: /a/b/c/mvtnorm.

You do need to tell R where to find that package once it’s installed, which you can do via the .libPaths() function:

> .libPaths("/a/b/c/")

This will add that new directory to the ones R was already using. If you use that directory often enough, you may wish to add that call to .libPaths() in your .Rprofile startup file in your home directory.

A call to .libPaths() without an argument will show you a list of all the places R will currently look for loading a package when requested.

Sometimes you need to install “by hand” to make modifications required to make a particular R package work on your system. The following example demonstrates how I did so in one particular instance, and it will serve as a case study on handling situations in which ordinary methods don’t work.

I wanted to install the Rmpi package on our department’s instructional machines in the directory /home/matloff/R. I tried using install.packages() first but found that the automated process could not find the MPI library on our machines. The problem was that R was looking for those files in /usr/local/lam, whereas I knew they were in /usr/local/LAM. Since these were public machines, not my own, I did not have the authority to change the name. So, I downloaded the Rmpi files in the packed form Rmpi_0.5-3.tar.gz. I unpacked that file in my directory ~/tmp, producing a directory named ~/tmp/Rmpi.

If I had not experienced this problem, at this point, I could have just typed the following in a terminal window from within the ~/tmp directory:

R CMD INSTALL -l /home/matloff/R  Rmpi

That command would install the package contained in ~/tmp/Rmpi, placing it in /home/matloff/R. This would have been an alternative to calling install.packages().

But as noted, I had to deal with a problem. Within the ~/tmp/Rmpi directory, there was a configure file, so I ran this command on my Linux command line:

configure --help

It told me that I could specify the location of my MPI files to configure, as follows:

configure --with-mpi=/usr/local/LAM

This applies if you run configure directly, but I ran it via R:

R CMD INSTALL -l /home/matloff/R  Rmpi --configure-args=--with-mpi=/usr/local/LAM

Well, that seemed to work, in the sense that R did install the package, but R also noted that it had a problem with the threads library on our machines. Sure enough, when I tried to load Rmpi, I got a runtime error, saying that a certain threads function wasn’t there.

I knew that our threads library was fine, so I went into the configure file and commented out two lines:

# if test $ac_cv_lib_pthread_main = yes; then
     MPI_LIBS="$MPI_LIBS -lpthread"
# fi

In other words, I forced it to use what I knew (or was fairly sure) would work. I then reran R CMD INSTALL, and the package loaded without any problems.