install.packages("glmnet")
6 R packages
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.****
6.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:
6.0.2 List installed packages
List all R packages installed on your system with installed.packages()
6.0.3 Check installed package’s version
packageVersion("rtemis")
[1] '0.97.0'
6.0.4 List outdated packages
6.0.5 Update installed packages
update.packages(ask = FALSE)
We use the ask = FALSE
argument to avoid being prompted before updating each package.
6.0.6 List attached packages
List attached packages with search()
:
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"
6.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")
You can then use remotes::install_github()
to install a package from GitHub, replacing user
and repo
with the GitHub username and repository name, respectively:
remotes::install_github("user/repo")
Note: Running remotes::install_github("user/repo")
will not reinstall a previously installed package, unless it has been updated.
6.1.1 Install a specific release
You can install a specific release of a package by appending @
followed by the version number.
remotes::install_github("user/[email protected]")
6.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")
6.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:
- macOS tools
- Windows Rtools
- Linux: use your systems package manager to install
gcc
6.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.
6.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.
6.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.
6.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.
6.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
6.7 R datasets
6.7.1 Datasets included with R
List built-in datasets with data()
and no arguments:
data()
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()
search()
[1] ".GlobalEnv" "package:stats" "package:graphics"
[4] "package:grDevices" "package:utils" "package:datasets"
[7] "package:methods" "Autoloads" "package:base"
6.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()
:
To load such a dataset:
data(Sonar, package = "mlbench")