- What is R packages?
- Installing R packages
- Load and use an R package
- View loaded R packages
- Remove installed packages
- Update installed packages
- Related articles
Here, we’ll describe:
- what is an R package
- and how to install and use R packages
What is 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.
After installation, you must first load the package for using the functions in the package.
Installing R packages
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:
For example, to install the package named readr, type this:
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 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:
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.
View the list of installed packages
To view the list of the already installed packages on your computer, type :
Note that, in RStudio, the list of installed packages are available in the lower right window under Packages tab (see the image below).
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.
Load and use an R package
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 data library("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 400m 1 SEBRLE 11.04 7.58 14.83 2.07 49.81 2 CLAY 10.76 7.40 14.26 1.86 49.37 3 KARPOV 11.02 7.30 14.77 2.04 48.37 4 BERNARD 11.02 7.23 14.25 1.92 48.93 5 YURKOV 11.34 7.09 15.19 2.10 50.42 6 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, use the function search():
 ".GlobalEnv" "package:readr" "package:stats" "package:graphics"  "package:grDevices" "package:utils" "package:datasets" "package:methods"  "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)
Remove installed packages
To remove an installed R package, use the function remove.packages() as follow:
Update installed packages
If you want to update all installed R packages, type this:
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
This analysis has been performed using R software (ver. 3.2.3).
Show me some love with the like buttons below... Thank you and please don't forget to share and comment below!!
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!
Want to Learn More on R Programming and Data Science?
Follow us by Email On Social Networks: