You are currently viewing How to Install and Update R Packages?

Use install.packages("package_name") to install package(s) in R and use update.packages("package_name") to update an already existing package(s). In this R programming article, I will explain how to install and update a single or multiple R package by installing dplyr package and data.table package from terminal and RStudio.

Advertisements

R packages are collections of pre-compiled and well-tested re-usable functions that are developed by the community. Packages are the basic blocks to extending and improving existing R base functionalities. All these R packages are present in the CRAN package repository which features almost 18000 plus packages and many more are publicly available through the internet.

1. Syntax of install.packages()

Following is the syntax of install.packages() function.


#Syntax of install.packages()
install.packages(x)

Here, x can be a single package name or a vector to install multiple packages.

2. Install R Packages

In order to install R packages, you can run the following command from the R terminal or from the RStudio. The following example installs the dplyr package into the R environment from the R terminal. When you are running from the terminal it prompts you with the mirrors to select, select the mirror that is closest to you to install.


#Install dplyr package
install.packages("dplyr")

When you install it from the terminal, you should see the following output.

R install package
Install R package

3. Install Multiple Packages

To install multiple packages use the vector with all the packages you wanted, to create a vector use c(). The following examples install dplyr and data.table packages. With this you don’t have to write the same statement again and again for each install, this comes in handy and helps reduce code by writing single-line statements.


# Install Multiple Packages
install.packages(c("dplyr","data.table"))

4. Update R Packages

To update an R package use update.packages() function. The following example updates the dplyr package with the latest version. Alternatively, you can also remove the R package and install it again.


# Update dplyr package
update.packages("dplyr")

Run the update package command from the R terminal as shown in below.

R update package
Update R package

5. Install R Packages from RStudio

If you prefer a graphical user interface to install R packages, both RStudio and the RGui are supported. In RStudio you will find it at Tools -> Install Package or select the Packages -> Install, this brings up a pop-up window to type the package you want to install.

With RStudio GUI you have an option to select like, from which repository you want to install, whether to install dependencies and where to install. If you don’t have GUI, I would recommend installing RStudio to write R Programming.

install packages rstudio
Install package from RStudio

6. Update R Package from RStudio

Similarly, to update an R package(s) from RStudio use Tools -> Check for Update Package or select the Packages -> Update. This will bring up a pop-up window similar to the below with the package name, installed version, and available new version. select the package you wanted to update and click on the Install Updates button.

update package
Update package from RStudio

7. Conclusion

In this article, you have learned how to install and update single and multiple packages from the R terminal and RStudio.

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium