Open In App

Updating Packages In R

Last Updated : 19 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

R is a statistical programming language that relies heavily on the packages and libraries it offers. These packages help with many problems by offering features that make the analysis easier. Updating these packages is crucial to use the new features, bug fixes, or improvements. In this article, we will learn how to update packages in R Programming Language.

Why Update Packages?

Updating packages in R is important due to several reasons:

  1. Bug Fixes: Updating packages can fix the existing bugs in the package that cause issues in running the code. In the updated version of the package, these bugs are fixed so that we can work easily.
  2. New Features: Keeping packages up-to-date helps us explore the new features introduced in these packages. These new features help us in our work.
  3. Performance Improvements: Updates packages run faster because the bugs and issues are fixed with the help of a better version. This reduces time consumption and helps with performance as well as accuracy.
  4. Compatibility: Based on the current R version we need to keep the package of the similar version of the package to run smoothly.
  5. Security: Updated R packages can update the security of our data and make the R environment more secure.

Before we update packages we need to check the current installed version of our package.

Checking Package Versions

To check the version of the currently installed package:

R




#syntax to check the version
installed_packages <- installed.packages()
installed_packages[, c("Package", "Version")]


Output:

                  Package             Version     
abind             "abind"             "1.4-5"     
ade4              "ade4"              "1.7-22"    
AICcmodavg        "AICcmodavg"        "2.3-3"     
askpass           "askpass"           "1.2.0"     
assertthat        "assertthat"        "0.2.1"     
backports         "backports"         "1.4.1"     
base              "base"              "4.3.1"     
base64enc         "base64enc"         "0.1-3"     
BBmisc            "BBmisc"            "1.13"      
BH                "BH"                "1.81.0-1"  
bigassertr        "bigassertr"        "0.1.6"     
bigparallelr      "bigparallelr"      "0.3.2"     
bigstatsr         "bigstatsr"         "1.5.12"    
BiocManager       "BiocManager"       "1.30.22"   
bit               "bit"               "4.0.5"     
bit64             "bit64"             "4.0.5"     
bitops            "bitops"            "1.0-7"     
blob              "blob"              "1.2.4"     
bmp               "bmp"               "0.3"   ..........

Updating Packages

To update the package in R we use the below-mentioned syntax. This compares the version that is currently installed and the updated version that is available.

R




update.packages(ask = FALSE, checkBuilt = TRUE)


when the ask parameter is set to “FALSE” it updates all the packages present without asking for permission for each one separately.
checkBuilt parameter checks the compatibility of the binary packages with the current version of R.

The above-given syntax updates all the packages that you have installed in R. We can specifically install a single package as well if we wish to.

Updating a Specific Package

To update a particular package in R we can use the below-mentioned syntax. We will understand this with the help of a package called ggplot2 used for visualization in R.

R




#syntax to update package
install.packages("package_name", dependencies = TRUE, update = TRUE)
#example usage
install.packages("ggplot2", dependencies = TRUE, update = TRUE)


Output:

package ‘wk’ successfully unpacked and MD5 sums checked
package ‘classInt’ successfully unpacked and MD5 sums checked
package ‘s2’ successfully unpacked and MD5 sums checked
package ‘units’ successfully unpacked and MD5 sums checked
package ‘ggplot2movies’ successfully unpacked and MD5 sums checked
package ‘hexbin’ successfully unpacked and MD5 sums checked
package ‘mapproj’ successfully unpacked and MD5 sums checked
package ‘maps’ successfully unpacked and MD5 sums checked
package ‘sf’ successfully unpacked and MD5 sums checked
package ‘svglite’ successfully unpacked and MD5 sums checked
package ‘vdiffr’ successfully unpacked and MD5 sums checked
package ‘ggplot2’ successfully unpacked and MD5 sums checked

Updating in R studio

We can directly update all packages or a specific package in R studio. To update a package in R we can follow the below given steps.

Step 1: Go to package section

First, go to the package section in R studio and select the “update” option present at the top left corner.

update-package-GFG-(1)

R studio

After clicking on the “update” option in R a window will appear.

Step 2: Select the option in the update package window

update-package-window-GFG

Update Package R studio

The options mentioned in this window can be used to update packages according to our needs. The “Select All” option at the bottom left corner of the window can help us select all the packages present in R to update all of them
Other than that we can update packages specifically by checking the tick box present adjacent to the names of individual packages.

Step 3: Choose the package to update

After selecting the package we want to update we can easily select the “Install Updates” option present at the bottom right corner to install updates. This will install all the updates present and the output will be present in the R console.

Updating from GitHub or Other Sources

We can also update packages from GitHub or any other repositories. To update packages from another source than CRAN we can follow the below-mentioned code:e can also update packages from GitHub or any other repositories. To update packages from another source than CRAN we can follow the below-mentioned code:.
To install updates from other sources we first need to install the “devtools” package in R. This package is used for the management and development of R packages. This package simplifies tasks for users making it easier to use, create, share, maintain, or update packages in R.

R




# Install devtools if not already installed
if (!requireNamespace("devtools", quietly = TRUE)) {
  install.packages("devtools")
}
 
# Load devtools
library(devtools)
 
# Update package from GitHub
devtools::install_github("username/package_name")


This can help us access features that are not available on CRAN and it keeps us up-to-date as well.

Conclusion

In this article, we learned how to update packages in R by different methods and why it is necessary to update these packages. We also learned how to update packages if they are not available in CRAN.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads