How to install packages in R? 3 simple methods
R is a programming language for statistical computing and graphics. It can be used in order to make complicated statistical models such as linear regression, and create astonishing visualizations. One of R’s greatest strengths is the ability to extend its capabilities through user-created packages.
Packages may include functions that enable statistical techniques, functionality, and graphic designers not included when first installing R. In this article I will demonstrate 3 ways to install packages in R:
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.
When using the install_packages() function, on the other hand, we are able to install more than one package. In order to use it, you’ll have to call the function and inside the parentheses add a list of packages names, each one between opening and closing quotation marks, as follows:
# 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 quick tip when installing multiple packages: to make your code cleaner and easier to read and change, I recommend using a vector or a list to store all the packages’ names, and then inserting the vector name into the install_packages() function. This should look as follows:
# 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:
In order to open this window, open Rstudio and make sure that on the bottom right pane, the packages tab is selected (highlighted in yellow):
And then press the install button (highlighted in yellow):
And then the following menu should appear on the screen:
In this article, I won’t go in-depth about each and every parameter in the menu. The only parameter you must obviously change is the Packages parameter.
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.
Choosing one of the two remaining methods over the other really depends on the type of programmer you are. Someone like myself, who likes to keep all the analysis process organized and well-documented would probably use the install_packages() method, but someone who might find it redundant might use the graphical interface option.