How to Install R Packages From Source

Jesse John Feb 02, 2024
  1. Use the install.packages() Function to Install R Packages From Source
  2. About R Packages
  3. Install R Packages From a Local Source File
  4. Install an Old Version of R Packages From CRAN
  5. Install Any R Package From Source
  6. Conclusion
How to Install R Packages From Source

R packages are created and distributed in source form. However, some packages contain C, C++, or Fortran code which needs to be compiled.

Therefore, binary packages are usually available and installed for Windows and macOS users. This article will learn how to use the install.packages() function to install two packages from a source, one from a local file and one from CRAN.

In practice, installing a package from a source may not be smooth. In some cases, it may not work at all.

Therefore, we will begin with some general details about the install.packages() function and about R packages. This information may help us if we get stuck.

Use the install.packages() Function to Install R Packages From Source

Generally, we use the install.packages() function with a single parameter: the package’s name we want to install, in double quotes. For example, install.packages("package_name").

The function checks the operating system (OS), the R version installed, and installs the latest version of the package on CRAN if it is compatible with our OS and R version.

For users of Windows and macOS operating systems, it installs the binary version of the package if it is available for the latest version of the package. For Linux users, packages are always installed from a source.

We can also use the install.packages() function to install a package from a local or online source file. The function has other arguments, some of which we will use when installing from a source.

  1. The repos argument gives the URLs of the repositories, for example, CRAN mirrors.
  2. The dependencies argument specifies the types of dependencies to install.
  3. The type argument specifies whether the installation should be from a source or a binary.

To install from source, we need to know how these arguments depend on each other.

The documentation of the install.packages() function states the following.

  1. The repos parameter can be NULL to install from local files, directories or URLs…..
  2. The dependencies parameter is “Not used if repos = NULL”.
  3. You may need to first manually install its dependencies when you are installing an old version of a package from its source on CRAN.

Section 6.3 of the R Installation and Administration Manual states: install.packages can install a source package from a local .tar.gz file (or a URL to such a file) by setting argument repos to NULL: this will be selected automatically if the name given is a single .tar.gz file..

Both imply that R will not fetch and install dependencies when using a source file for installation.

About R Packages

R comes with very basic functionality built into it. It gets most of its power from packages that R users have developed over the past few decades.

These packages are made available on The Comprehensive R Archive Network, CRAN.

Visit the CRAN website, click on Packages in the sidebar, and click on Table of available packages (sorted by date or name). The list of all available packages is displayed.

Click on the name of any package. The CRAN page of that package opens.

We will consider the example of a popular package, dplyr. Search for the package name dplyr in that list, and click on the link.

Note the following details on the CRAN page of dplyr.

  1. Depends shows the minimum version of R that this package will work on.
  2. Imports show the packages that dplyr depends on. dplyr will get R to install them if we install them the usual way.
  3. Needs compilation shows that the package source has C, C++, or Fortran code that needs to be compiled.
  4. Under Downloads, there are four lines with links.
    1. Package source.
    2. Windows binaries.
    3. macOS binaries.
    4. Old sources.

For installing a package from a source, we need to be aware of two specific details.

  1. The packages are listed under Imports. The package that we want to install depends on all of these.

When installing from source, we need first to install all these packages. Each of these packages may also depend on other packages.

  1. Whether the source includes code that needs to be compiled, we need to have the relevant tools installed on our system.

The documentation for the install.packages() function gives the requirements in the Binary packages section.

Also, remember that a few packages are only available for some operating systems. Such packages have a description for OS_type on their CRAN page. For example, look at the blatr package.

Install R Packages From a Local Source File

To install a package from a local source file, we will use the file name rather than the package name as the first argument to the install.packages() function. We will also set type = "source" and repos = NULL.

First, let us get an R package in source form. Download the source file of package aplore3 from CRAN.

Why this particular package? The CRAN page of this package shows that it does not import any other package and does not need compilation.

Therefore, we can expect the installation from a source to proceed without a glitch.

Place the downloaded .tar.gz file in the working directory of R. Then run the following command to install the package from a source.

Example Code:

# FIRST, DOWNLOAD THE FILE.
# Follow the instructions to download the file.


# Make sure R can find this file.
# Place it in the working directory.
# Or change the working directory to where this file is.


# CHANGE THE FILENAME IN THE FOLLOWING CODE
# to match your downloaded file.
install.packages("aplore3_0.9.tar.gz", type = "source", repos = NULL)

R will display DONE and the package name in parentheses. It indicates that the package is installed successfully.

To use the package, we need to load the package with the library() function.

Install an Old Version of R Packages From CRAN

If the latest version of a package depends on a newer version of R than what we have, we can install an older version from a source by using the URL of the older version as the first parameter to the install.packages() function.

We can download the old source file and install it like in the previous section. But for demonstration, we will use the URL.

Remember that R will not fetch and install dependencies even when using a URL. So we need to select a package with no dependencies or one for which all dependencies are already installed.

We also need to be mindful of whether the package needs compilation. If yes, our system has to meet the requirements.

We will install an old version of the pps package for demonstration. This package does not depend on other packages and does not need compilation.

We will copy the URL of the 2012 version of the source from CRAN and use it in the code.

Example Code:

install.packages("https://cran.r-project.org/src/contrib/Archive/pps/pps_0.94.tar.gz", repos = NULL, type = "source")

Install Any R Package From Source

The package and our system (OS, R, other software) must meet the two requirements stated in the previous sections and be repeated here for convenience to install any other package from a source.

  1. The package does not depend on other packages (nothing is listed under Imports), or all dependencies are already installed, or we can first install all the required dependencies.
  2. The package does not need compilation, or our system has the required tools for this purpose.

Many packages do not require compilation, but most depend on other packages.

References and Help for Installing R Packages

The R Project gives detailed platform-specific instructions for installing packages in Section 6.3 of the R Installation and Administration manual.

The documentation of the install.packages() function also provides platform-specific details. In R Studio, click Help > Search R Help and type install.packages in the search box (without parentheses).

Alternately, type a question mark followed by the function name at the R console.

R’s error messages during package installation often mention the exact requirement not met because the installation failed.

At a more fundamental level, we must ensure that the file name is typed correctly (including capitalization), the function’s syntax is correct, and R can find the source package.

Conclusion

R’s install.packages() function does a lot of work when used to install the latest version of a package from CRAN using the package name. CRAN has compiled versions of most packages for Windows and macOS users also helps.

When using the same function to install a package from a source file, we must first install dependencies and ensure that our system can compile C, C++, or Fortran code.

Author: Jesse John
Jesse John avatar Jesse John avatar

Jesse is passionate about data analysis and visualization. He uses the R statistical programming language for all aspects of his work.