Installing and Using R Packages - Easy Guides - Wiki (2024)

  • Home
  • Explorer
    • What is R packages?
    • Installing R packages
      • Install a package from CRAN
      • Install a package from Bioconductor
      • Install a package from Github
      • View the list of installed packages
      • Folder containing installed packages
    • Load and use an R package
    • View loaded R packages
    • Remove installed packages
    • Update installed packages
    • Summary
    • Related articles
    • Infos

    In our previous articles, we published i) guides for installing and launching R/RStudio, ii) the basics of R programming, and ii) guides for finding help in R.


    Here, we’ll describe:

    • what is an R package
    • and how to install and use R packages

    An R package is an extension of R containing data sets and specific functions to solve specific questions.

    R comes with standard (or base) packages, which contain the basic functions and data sets as well as standard statistical and graphical functions that allow R to work.

    There are also thousands other R packages available for download and installation from CRAN, Bioconductor and GitHub repositories.

    After installation, you must first load the package for using the functions in the package.

    Packages can be installed either from CRAN (for general packages), from Bioconductor (for biology-related packages) or from Github (developing versions of packages).

    Install a package from CRAN

    The function install.packages() is used to install a package from CRAN. The syntax is as follow:

    install.packages("package_name")

    For example, to install the package named readr, type this:

    install.packages("readr")

    Note that, every time you install an R package, R may ask you to specify a CRAN mirror (or server). Choose one that’s close to your location, and R will connect to that server to download and install the package files.

    It’s also possible to install multiple packages at the same time, as follow:

    install.packages(c("readr", "ggplot2"))

    Install a package from Bioconductor

    Bioconductor contains packages for analyzing biological related data. In the following R code, we want to install the R/Bioconductor package limma, which is dedicated to analyse genomic data.

    To install a package from Bioconductor, use this:

    source("https://bioconductor.org/biocl*te.R")biocl*te("limma")

    Install a package from Github

    GitHub is a repository useful for all software development and data analysis, including R packages. It makes sharing your package easy. You can read more about GitHub here: Git and GitHub, by Hadley Wickham.

    To install a package from GitHub, the R package devtools (by Hadley Wickham) can be used. You should first install devtools if you don’t have it installed on your computer.

    For example, the following R code installs the latest version of survminer R package developed by A. Kassambara (https://github.com/kassambara/survminer).

    install.packages("devtools")devtools::install_github("kassambara/survminer")

    View the list of installed packages

    To view the list of the already installed packages on your computer, type :

    installed.packages()

    Note that, in RStudio, the list of installed packages are available in the lower right window under Packages tab (see the image below).

    Installing and Using R Packages - Easy Guides - Wiki (1)

    Folder containing installed packages

    R packages are installed in a directory called library. The R function .libPaths() can be used to get the path to the library.

    .libPaths()
    [1] "/Library/Frameworks/R.framework/Versions/3.2/Resources/library"

    To use a specific function available in an R package, you have to load the R package using the function library().

    In the following R code, we want to import a file (“http://www.sthda.com/upload/decathlon.txt”) into R using the R package readr, which has been installed in the previous section.

    The function read_tsv() [in readr] can be used to import a tab separated .txt file:

    # Import my datalibrary("readr")my_data <- read_tsv("http://www.sthda.com/upload/decathlon.txt")# View the first 6 rows and tge first 6 columns# syntax: my_data[row, column]my_data[1:6, 1:6]
     name 100m Long.jump Shot.put High.jump 400m1 SEBRLE 11.04 7.58 14.83 2.07 49.812 CLAY 10.76 7.40 14.26 1.86 49.373 KARPOV 11.02 7.30 14.77 2.04 48.374 BERNARD 11.02 7.23 14.25 1.92 48.935 YURKOV 11.34 7.09 15.19 2.10 50.426 WARNERS 11.11 7.60 14.31 1.98 48.68

    To view the list of loaded (or attached) packages during an R session, use the function search():

    search()
     [1] ".GlobalEnv" "package:readr" "package:stats" "package:graphics" [5] "package:grDevices" "package:utils" "package:datasets" "package:methods" [9] "Autoloads" "package:base" 

    If you’re done with the package readr and you want to unload it, use the function detach():

    detach("readr", unload = TRUE)

    To remove an installed R package, use the function remove.packages() as follow:

    remove.packages("package_name")

    If you want to update all installed R packages, type this:

    update.packages()

    To update specific installed packages, say readr and ggplot2, use this:

    update.packages(oldPkgs = c("readr", "ggplot2"))

    • install.packages(“package_name”): Install a package

    • library(“package_name”): Load and use a package

    • detach(“package_name”, unload = TRUE): Unload a package

    • remove.packages(“package_name”): Remove an installed package from your computer

    • update.packages(oldPkgs = “package_name”): Update a package
    • Previous chapters
      • What’is R and why learning R?
      • Installing R and RStudio
      • Running RStudio and setting up your working directory
      • R programming basics
      • Getting help with functions in R programming
    • Next chapters
      • R Built-in data sets
      • Importing data into R
      • Exporting data from R

    This analysis has been performed using R software (ver. 3.2.3).


    Enjoyed this article? I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter, Facebook or Linked In.

    Show me some love with the like buttons below... Thank you and please don't forget to share and comment below!!

    Avez vous aimé cet article? Je vous serais très reconnaissant si vous aidiez à sa diffusion en l'envoyant par courriel à un ami ou en le partageant sur Twitter, Facebook ou Linked In.

    Montrez-moi un peu d'amour avec les like ci-dessous ... Merci et n'oubliez pas, s'il vous plaît, de partager et de commenter ci-dessous!



    Recommended for You!


    Machine Learning Essentials: Practical Guide in R
    Practical Guide to Cluster Analysis in R
    Practical Guide to Principal Component Methods in R
    R Graphics Essentials for Great Data Visualization
    Network Analysis and Visualization in R
    More books on R and data science

    Recommended for you

    This section contains best data science and self-development resources to help you on your path.

    Coursera - Online Courses and Specialization

    Data science

    Popular Courses Launched in 2020

    Trending Courses

    Books - Data Science

    Our Books

    Others



    Want to Learn More on R Programming and Data Science?

    Follow us by Email

    On Social Networks:

    Get involved :
    Click to follow us on Facebook and Google+ :
    Comment this article by clicking on "Discussion" button (top-right position of this page)

    Installing and Using R Packages - Easy Guides - Wiki (2024)

    References

    Top Articles
    Latest Posts
    Article information

    Author: Tish Haag

    Last Updated:

    Views: 6087

    Rating: 4.7 / 5 (47 voted)

    Reviews: 94% of readers found this page helpful

    Author information

    Name: Tish Haag

    Birthday: 1999-11-18

    Address: 30256 Tara Expressway, Kutchburgh, VT 92892-0078

    Phone: +4215847628708

    Job: Internal Consulting Engineer

    Hobby: Roller skating, Roller skating, Kayaking, Flying, Graffiti, Ghost hunting, scrapbook

    Introduction: My name is Tish Haag, I am a excited, delightful, curious, beautiful, agreeable, enchanting, fancy person who loves writing and wants to share my knowledge and understanding with you.