5  R packages

Note

The evaluation of most code blocks in this page has been disabled to avoid either reinstalling packages or producing long output, which is why most code blocks are not followed by any output.****

5.0.1 Install package from CRAN

The Comprehensive R Archive Network (CRAN) is the official R package repository and currently hosts 19930 packages (as of 2023-09-27). To install a package from CRAN, use the builtin install.packages() command:

5.0.2 List installed packages

List all R packages installed on your system with installed.packages()

5.0.3 Check installed package’s version

packageVersion("rtemis")
[1] '0.96.1'

5.0.4 List outdated packages

5.0.5 Update installed packages

update.packages(ask = FALSE)
Tip

We use the ask = FALSE argument to avoid being prompted before updating each package.

5.0.6 List attached packages

List attached packages with search():

[1] ".GlobalEnv"        "package:stats"     "package:graphics" 
[4] "package:grDevices" "package:utils"     "package:datasets" 
[7] "package:methods"   "Autoloads"         "package:base"     

List attached packages with their system path:

[1] ".GlobalEnv"                                                                    
[2] "/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/stats"    
[3] "/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/graphics" 
[4] "/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/grDevices"
[5] "/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/utils"    
[6] "/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/datasets" 
[7] "/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/methods"  
[8] "Autoloads"                                                                     
[9] "/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/base"     

5.1 GitHub

GitHub contains a large number of R packages, some of which also exist in CRAN, but the GitHub version may be updated (a lot) more frequently. To install from GitHub, you need to have the remotes package from CRAN first:

install.packages("remotes")
remotes::install_github("user/repo")

Note: Running remotes::install_github("user/repo") will not reinstall a previously installed package, unless it has been updated.

5.2 Bioconductor

Bioconductor is a repository which includes tools for the analysis and comprehension of high-throughput genomic data, among others. To install packages from Bioconductor, first install the BiocManager package from CRAN. See Bioconductor’s latest installation instructions here; learn more about BiocManager here

install.packages("BiocManager")

For R versions 4.2+, proceed by installing Bioconductor:

BiocManager::install(version = "3.15")

Now, you can use BiocManager::install() to install Bioconductor packages:

BiocManager::install("packageName")

5.3 Installation from source

R packages may include only R code, or may also include code in other languages, like C, C++, and Fortran, which need compilation. All CRAN packages are available in binary format for all supported operating systems. However, if you want to install a newer or development version that is not yet available in binary format, R can compile and install the package for you, provided the appropriate compiler is installed on your system.

If you need to install compiler tools for your system, follow the instructions:

5.4 Dependencies

Most R packages, whether in CRAN, Bioconductor, or GitHub, themselves rely on other packages to run. These are called dependencies. Many of these dependencies get installed automatically when you call install.packages() or remotes::install_github(), etc. This depends largely on whether they are essential for the new package to work. Some packages, especially if they provide a large number of functions that may not all be used by all users, may make some dependencies optional. In that cases, if you try to execute a specific function that depends on uninstalled packages you may get a warning or error or some type of message indicating that you need to install further packages.

5.5 Project-level dependency management

Advanced users may benefit from using a project-level set of dependencies that can be saved, recalled, and updated at will. The renv package is the latest approach to providing such functionality. Follow the introduction vignette here.

5.6 Builtin Documentation

After you’ve successfully installed R and RStudio, one of the first things to know is how to access and search the builtin documentation.

5.6.1 Get help on a specific item

If you know the name of what you’re looking for (an R function most commonly, but possibly also the name of a dataset, or a package itself), just type ? followed by the name of said function, dataset, etc. in the R prompt:

?sample

In RStudio, the above example will bring up the documentation for the sample function in the dedicated “Help” window, commonly situated at the bottom right (but can be moved by the user freely). If you are running R directly at the system shell, the same information is printed directly at the console.
Try running the above example on your system.

5.6.2 Search the docs

If you do not know the name of what you are looking for, you can use double question marks, ??, followed by your query (this is short for the help.search command that provides a number of arguments you can look up using ?help.search):

??bootstrap

5.7 R datasets

5.7.1 Datasets included with R

List built-in datasets with data() and no arguments:

These built-in datasets are normally readily available in the R console because the datasets package is automatically loaded at startup. You can list all loaded packages with search()

[1] ".GlobalEnv"        "package:stats"     "package:graphics" 
[4] "package:grDevices" "package:utils"     "package:datasets" 
[7] "package:methods"   "Autoloads"         "package:base"     

5.7.2 Datasets included with other packages

Many R packages come with included datasets. If available, we can list them using the package argument of data():

data(package = "glmnet")
data(package = "MASS")
data(package = "mlbench")

To load such a dataset:

data(Sonar, package = "mlbench")