This page looks best with JavaScript enabled

#6 How to install an R package (basics)

 ·  ☕ 3 min read

Finding and installing the desired package is priceless. But how do you install a package? It is not so simple when install.packages() is not enough. In this article I want to share the main methods of installing packages starting from the simplest. But let’s get down to business.

Install from CRAN

The standard method

1
install.package("dplyr")

Sometimes I want to specify some parameters. In this case, among other things, the command becomes non-interactive, therefore suitable for an automated script.

1
install.packages("rmarkdown", repos="http://cran.rstudio.com/")

Install if not available

However in these cases, the package is always installed, and what if I want to launch the installation only if the package is not already present? (In my opinion, however, it is better not to mix the analysis code with the environment preparation code)

1
if (require('dplyr')) install.packages('dplyr', repos='http://cran.rstudio.com/')"

Install from Github.com

What is Github.com?

Github.com is a hosting site for software projects. It is mainly used by developers, who through this portal can collaborate by exchanging ideas along with their implementations and organize the development of open source projects collaboratively. The great effectiveness that this tool provides to dev teams means that the new versions of the packages we need so much are born here. And in this sense Github is the cradle of all the most important innovations of the Open Source functionality of R.

So if you are looking for a new feature or a bug fix, this is the place to find the solution most easily.

How you do it?

Simple: user and name of the repository (this is the name of the project on Github) are the coordinates you need. For example, if you want to install the freshest version of dplyr you can find it at http://github.com/tidyverse/dplyr.

Coordinates: user = tidyverse, repository = dplyr

1
remotes::install_github("tidyverse/dplyr")

Pre-requisite: the installation of remotes. How you do it? Hint: use install.packages.

Remotes has itself a repository on Github: https://github.com/r-lib/remotes.

Other Git Hostings

Github.com chooses its name to indicate it is a Git hosting. Git is the Open Source software that allows you to version the source code. There are numerous hosting equivalents to Github: https://gitlab.com, https://bitbucket.org, etc… For this remotes provides special functions.

Install from local file

But what if the network is missing? Or if the package is private and I don’t have an online hosting to deposit it? An R package travels on-line as a file with extension .tar.gz or .zip and in order to install it:

1
install.packages("./MyPackage_2.3.tar.gz", repo = NULL)

The repo = NULL statement is essential, otherwise the package is still searched on-line.

Install a package on Windows OS

Windows expects precompiled packages. So if the package is not precompiled, in the installation command it is necessary to specify that it is a source file. Note that this is the default behaviour on Unix systems.

1
install.packages(... , type = "source")

Credits

Javapoint, CC BY-SA 4.0, via Wikimedia Commons

Share on