The package is an appropriate way to organize the work and share it with others. Typically, a package will include code (not only R code!), documentation for the package and the functions inside, some tests to check everything works as it should, and data sets. Packages in R language are a set of R functions, compiled code, and sample data. These are stored under a directory called “library” within the R environment. By default, R installs a group of packages during installation. Once we start the R console, only the default packages are available by default. Other packages that are already installed need to be loaded explicitly to be utilized by the R program that’s getting to use them.
What are Repositories?
A repository is a place where packages are located and stored so you can install packages from it. Organizations and Developers have a local repository, typically they are online and accessible to everyone. Some of the most popular repositories for R packages are:
- CRAN: Comprehensive R Archive Network(CRAN) is the official repository, it is a network of ftp and web servers maintained by the R community around the world. The R community coordinates it, and for a package to be published in CRAN, the Package needs to pass several tests to ensure that the package is following CRAN policies.
Bioconductor: Bioconductor is a topic-specific repository, intended for open source software for bioinformatics. Similar to CRAN, it has its own submission and review processes, and its community is very active having several conferences and meetings per year in order to maintain quality.
- Github: Github is the most popular repository for open source projects. It’s popular as it comes from the unlimited space for open source, the integration with git, a version control software, and its ease to share and collaborate with others.
Install an R Package
There are multiple ways to install R Package, some of them are,
Installing Packages From CRAN: For installing Package from CRAN we need the name of the package and use the following command:
Installing Package from CRAN is the most common and easiest way as we just have to use only one command. In order to install more than a package at a time, we just have to write them as a character vector in the first argument of the
Installing Bioconductor Packages: In Bioconductor, the standard way to install a package is by first executing the following script:
This will install some basic functions which are needed to install bioconductor packages, such as the
biocLite()function. To install the core packages of Bioconductor just type it without further arguments:
If we just want a few particular packages from this repository then type their names directly as a character vector:
Update, Remove and Check Installed Packages
- To check what packages are installed on your computer, type this command:
- To update all the packages, type this command:
- To update a specific package, type this command:
Installing Packages Using RStudio UI
In R Studio goto Tools -> Install Package, and there we will get a pop-up window to type the package you want to install:
Under Packages, type, and search Package which we want to install and then click on install button.
How to Load Packages
When a package is installed, we are ready to use its functionalities. If we just need a sporadic use of a few functions or data inside a package we can access them with the following notation
Let’s access births function of package babynames. Then type this command,
Difference Between a Package and a Library
There is always a confusion between a package and a library, and we find people calling libraries as packages.
library(): It is the command used to load a package, and it refers to the place where the package is contained, usually a folder on our computer.
- Package: It is the collection of functions bundled conveniently. The package is an appropriate way to organize our own work and share it with others.
Load More Than One Package at a Time
We can just input a vector of names to the
install.packages() function to install a package, in the case of the
library() function, this is not possible. We can load a set of packages one at a time, or if you prefer, use one of the many work arounds developed by R users.
Unload a Package
To unload a given package, use the
detach() function. The use will be:
detach("package:babynames", unload = TRUE)
Choose the Right R Packages
The traditional way of discovering packages is just by learning R, in many tutorials and courses the most popular packages are usually mentioned and used. The first alternative can be to browse categories of CRAN packages. CRAN is the official repository, also gives us the option to browse through packages.
Another alternative to finding packages can be R Documentation, a help documentation aggregator for R packages from CRAN, BioConductor, and GitHub, which offers you a search box ready for your requests directly on the main page.
- Grid and Lattice Packages in R Programming
- Get a List of all the Attached Packages in R Programming - search() Function
- Get the Full Path of all the Attached Packages in R Programming - searchpath() Function
- Packages in Perl
- Packages In Scala
- tidyr Package in R Programming
- Clustering in R Programming
- Concept of Comments in Computer Programming
- Modular Approach in Programming
- How to read Competitive Programming Questions?
- Introduction of Programming Paradigms
- How can competitive programming help you get a job?
- R - Object Oriented Programming
- Perl | Socket Programming
- Perl | CGI Programming
- The complete History of Java Programming Language
- Interesting facts about Ruby Programming Language
- Data Reshaping in R Programming
- Plotting Graphs using Two Dimensional List in R Programming
- Web Programming in C++
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.