How to install packages in R? 3 simple methods

1. Installing 1 package at a time: using the install_package() function

This function is one of the functions installed along with installing R, so if you’ve installed R — you should have this function without doing anything else. In order to use it, you’ll have to call the function and inside the parentheses add the name of the package, between the opening and closing quotation marks, as follows:

# A generic template. Replace the text inside the quotation marks with the name
# of the package you would like to install.
install_package("<name_of_package>")

# Example 1: installing a package called ggplot2
install_package("ggplot")

2. Installing 1 or more packages at a time: using the install_packages() function

Though the previous function and this one have similar names, one major difference is the number of packages we can install using the function. When using the install_package() function, we can install only one package at a time. If, for the sake of the example, we would like to install 10 packages — we would have to call this function 10 times.

# A generic template. Replace the text inside the quotation marks with the name
# of the packages you would like to install.
install_packages("<name_of_package_1>", "<name_of_package_2>", ... , "<name_of_package_n>")

# Example 2: installing 3 packages (ggplot2, magrittr, dplyr)
install_packages("ggplot", "magrittr", "dplyr")
# A generic template. Replace the text inside the quotation marks with the name
# of the packages you would like to install.
new_packages_to_install_names <- c("<name_of_package_1>", "<name_of_package_2>", ... , "<name_of_package_n>")
install_packages(new_packages_to_install_names)

# Example 3: installing 3 packages (ggplot2, magrittr, dplyr) using a vector
new_packages_to_install_names <- c("ggplot", "magrittr", "dplyr")
install_packages(new_packages_to_install_names)

2. Installing 1 or more packages at a time: using Rstudio’s graphical interface

If you fill uncomfortable using code and want an alternative way to install one or more packages, you can use Rstudio’s graphical interface. In order to do it, we would use the following menu:

Packages installation graphical interface.
Packages installation graphical interface
The first step in opening the package installation menu: selecting the package tab in the bottom right tab
The first step in opening the package installation menu: selecting the package tab in the bottom right tab
The second step in opening the package installation menu: select the install button
The second step in opening the package installation menu: select the install button
Packages installation graphical interface.
Packages installation graphical interface

What method should you choose?

I would argue that in order to find the most suitable method for you, you should not even consider using the first method in this article, because of its obvious limitations.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store