Installing and Using R Packages

  • What is R packages?
  • Installing R packages
    • Install a packet 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 parcel
  • 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 two) guides for finding assistance in R.


Here, we'll depict:

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

What is R packages?

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

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

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

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

Installing R packages

Packages can be installed either from CRAN (for general packages), from Bioconductor (for biological science-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 equally follow:

                    install.packages("package_name")                  

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

                    install.packages("readr")                  

Note that, every time y'all install an R bundle, R may ask yous to specify a CRAN mirror (or server). Choose 1 that's shut to your location, and R will connect to that server to download and install the package files.

It'due south also possible to install multiple packages at the aforementioned 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 lawmaking, we want to install the R/Bioconductor package limma, which is dedicated to analyse genomic data.

To install a parcel from Bioconductor, use this:

                    source("https://bioconductor.org/biocLite.R") biocLite("limma")                  

Install a package from Github

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

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

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 listing of installed packages

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

                    installed.packages()                  

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

installed packages, RStudio

Folder containing installed packages

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

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

Load and use an R bundle

To employ a specific function available in an R packet, you take to load the R package using the role 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 data library("readr") my_data <- read_tsv("http://www.sthda.com/upload/decathlon.txt") # View the first 6 rows and tge first vi columns # syntax: my_data[row, column] my_data[ane:6, 1:6]                
                                      name  100m Long.jump Shot.put High.jump  400m i  SEBRLE 11.04      7.58    14.83      ii.07 49.81 2    CLAY 10.76      7.40    fourteen.26      one.86 49.37 3  KARPOV 11.02      7.30    fourteen.77      2.04 48.37 four BERNARD 11.02      vii.23    14.25      1.92 48.93 5  YURKOV 11.34      vii.09    15.19      2.ten 50.42 half-dozen WARNERS 11.11      7.60    14.31      1.98 48.68                

View loaded R packages

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

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

If you're done with the package readr and you lot want to unload information technology, use the function detach():

                  detach("readr", unload = True)                

Remove installed packages

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

                  remove.packages("package_name")                

Update installed packages

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

Summary


  • install.packages("package_name"): Install a package

  • library("package_name"): Load and use a package

  • detach("package_name", unload = TRUE): Unload a packet

  • remove.packages("package_name"): Remove an installed parcel from your computer

  • update.packages(oldPkgs = "package_name"): Update a package

Infos

This analysis has been performed using R software (ver. 3.two.iii).


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

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

Avez vous aimé cet commodity? 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 northward'oubliez pas, south'il vous plaît, de partager et de commenter ci-dessous!