Built with Tutify.io

    AIStudyLabs

    How to Install and Update R Packages?

    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.

    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.

    <a id="h-1-syntax-of-install-packages"></a>

    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.

    <a id="h-2-install-r-packages"></a>

    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.

    <a id="h-3-install-multiple-packages"></a>

    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"))
    

    <a id="h-4-update-r-packages"></a>

    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.

    <a id="h-5-install-r-packages-from-rstudio"></a>

    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.

    <a id="h-6-update-r-package-from-rstudio"></a>

    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.

    <a id="h-7-conclusion"></a>

    7. Conclusion

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

    <a id="h-related-articles"></a>

    Related Articles