banner



How To Check If A Package Is Installed In R

An efficient fashion to install and load R packages

  • What is a R package and how to apply it?
  • Inefficient fashion to install and load R packages
  • More efficient manner
  • Most efficient way
    • {pacman} package
    • {librarian} package

What is a R package and how to utilize it?

Dissimilar other programs, only fundamental functionalities come up past default with R. You will thus oftentimes need to install some "extensions" to perform the analyses you lot want. These extensions which are are collections of functions and datasets developed and published past R users are called packages. They extend existing base R functionalities by adding new ones. R is open source so everyone can write code and publish it equally a parcel, and everyone tin can install a bundle and get-go using the functions or datasets congenital inside the package, all this for free.

In order to use a package, information technology needs to be installed on your estimator past running install.packages("name_of_package") (do non forget "" around the name of the parcel, otherwise R will expect for an object saved under that name!). In one case the parcel is installed, you lot must load the package and only after it has been loaded y'all can use all the functions and datasets it contains. To load a package, run library(name_of_package) (this time "" effectually the name of the package are optional, but can withal be used if y'all wish).

Inefficient mode to install and load R packages

Depending on how long y'all have been using R, you lot may use a limited corporeality of packages or, on the contrary, a large amount of them. As you apply more and more packages y'all will soon start to take (too) many lines of code just for installing and loading them.

Here is a preview of the lawmaking from my PhD thesis showing how the installation and loading of R packages looked like when I started working on R (just a fraction of them are displayed to shorten the code):one

              # Installation of required packages install.packages("tidyverse") install.packages("ggplot2") install.packages("readxl") install.packages("dplyr") install.packages("tidyr") install.packages("ggfortify") install.packages("DT") install.packages("reshape2") install.packages("knitr") install.packages("lubridate")  # Load packages library("tidyverse") library("ggplot2") library("readxl") library("dplyr") library("tidyr") library("ggfortify") library("DT") library("reshape2") library("knitr") library("lubridate")            

Equally yous can guess the code became longer and longer every bit I needed more and more packages for my analyses. Moreover, I tended to reinstall all packages as I was working on iv unlike computers and I could not remember which packages were already installed on which machine. Reinstalling all packages every time I opened my script or R Markdown certificate was a waste of time.

More than efficient way

Then one day, a colleague of mine shared some of his code with me. I am glad he did equally he introduced me to a much more than efficient way to install and load R packages. He gave me the permission to share the tip, so hither is the code I now use to perform the task of installing and loading R packages:

              # Package names packages <- c("ggplot2", "readxl", "dplyr", "tidyr", "ggfortify", "DT", "reshape2", "knitr", "lubridate", "pwr", "psy", "car", "doBy", "imputeMissings", "RcmdrMisc", "questionr", "vcd", "multcomp", "KappaGUI", "rcompanion", "FactoMineR", "factoextra", "corrplot", "ltm", "goeveg", "corrplot", "FSA", "MASS", "scales", "nlme", "psych", "ordinal", "lmtest", "ggpubr", "dslabs", "stringr", "help", "ggstatsplot", "forcats", "styler", "remedy", "snakecaser", "addinslist", "esquisse", "hither", "summarytools", "magrittr", "tidyverse", "funModeling", "pander", "cluster", "abind")  # Install packages non yet installed installed_packages <- packages %in% rownames(installed.packages()) if (whatsoever(installed_packages == FALSE)) {   install.packages(packages[!installed_packages]) }  # Packages loading invisible(lapply(packages, library, character.merely = TRUE))            

This lawmaking for installing and loading R packages is more efficient in several ways:

  1. The function install.packages() accepts a vector as argument, and so i line of lawmaking for each packet in the past is now i line including all packages
  2. In the second part of the code, information technology checks whether a packet is already installed or not, then install simply the missing ones
  3. Regarding the packages loading (the last part of the code), the lapply() function is used to call the library() function on all packages at once, which makes the lawmaking more condense.
  4. The output when loading a bundle is rarely useful. The invisible() function removes this output.

From that day on, every time I demand to use a new package, I simply add together it to the vector packages at the top of the code, which is located at the top of my scripts and R Markdown documents. No matter on which computer I am working on, running the entire code will install only the missing packages and will load all of them. This greatly reduced the running time for the installation and loading of my R packages.

Most efficient way

{pacman} packet

Later this article was published, a reader informed me about the {pacman} package. After having read the documentation and attempt it out myself, I learned that the function p_load() from {pacman} checks to encounter if a package is installed, if not it attempts to install the package and and so loads it. It can likewise be applied to several packages at one time, all this in a very condensed way:

                install.packages("pacman")  pacman::p_load(ggplot2, tidyr, dplyr)              

Find more about this package on CRAN.

{librarian} packet

Like {pacman}, the shelf() function from the {librarian} parcel automatically installs, updates, and loads R packages that are not nevertheless installed in a single function. The function accepts packages from CRAN, GitHub, and Bioconductor (only if Bioconductor'southward Biobase package is installed). The function also accepts multiple package entries, provided as a comma-separated list of unquoted names (so no "" effectually bundle names).

Last just not least, the {librarian} packet allows to load packages automatically at the start of every R session (cheers to the lib_startup() role) and search for new packages on CRAN by keywords or regular expressions (thanks to the browse_cran() role).

Hither is an example of how to install missing packages and load them with the shelf() function:

                # From CRAN: install.packages("librarian")  librarian::shelf(ggplot2, DesiQuintans / desiderata, pander)              

For CRAN packages, provide the package proper noun as normal without "" and for GitHub packages, provide the username and parcel proper name separated by / (i.e., UserName/RepoName as shown for the desiderata package).

Find more about this package on CRAN.

Thanks for reading. I promise the article helped you to install and load R packages in a more than efficient way.

As always, if you take a question or a suggestion related to the topic covered in this article, please add it as a comment and so other readers can benefit from the discussion.

A special thanks Danilo and James for informing me about the {pacman} and {librarian} packages.



Liked this post?

Get updates every time a new article is published.
No spam and unsubscribe someday.

Share on:

Source: https://statsandr.com/blog/an-efficient-way-to-install-and-load-r-packages/

Posted by: strainhusafter1999.blogspot.com

0 Response to "How To Check If A Package Is Installed In R"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel