The post is about some important R package questions and answers. The R Package Questions and Answers are about how to load, install, and remove an R package.
Table of Contents
R Package Questions and Answers
Question: What is an R Package?
Answer: The r package is a collection of objects that the R Language can use. A package contains functions, data sets, and documentation (which helps how to use the package) or other objects such as dynamically loaded libraries of already compiled code.
Question: How do I see which packages I have available?
Answer: To see which packages you have to use the command at the R prompt
library()
Question: Which packages do I already have?
Answer: To see what packages are installed, one can use the installed.packages() command an R prompt. The output will show the packages installed.
installed.packages() installed.packages()[1:5,]
Loading R Packages
Question: How can one load a Package in R language?
Answer: Basic packages are already loaded. If you want to load a downloaded version of packages, use the command
library("package name") library("car")
where the package name is the name of the package you want to load. Here in the example, we used the “car”, which means the “car” package will be loaded.
Getting Help in R Language
Question: How can one see the documentation of a particular package?
Answer: To see the documentation of a particular package, use the command
library(help="package name") help(package="package name") help(package="car") library(help="car")
For more information about getting help, follow the link: Getting Help in R Language
Question: How do I see the help for a specific function?
Answer: To get help with a function in R, use the command
help("function name") ? function name ?Manova help("Manova")
Question: What functions and datasets are available in a package?
Answer: To check what functions and datasets are in a package, use the help command at the R prompt. This will provide package information, giving a list of functions and datasets.
help(package = "MASS")
Note that once a package is loaded, the help command can also be used with all available functions and datasets.
Installing and Removing R Packages
Question: How can one add or delete a package?
Answer: A package can be installed using the command
install.packages("package name")
and a package can be removed or deleted using the command
remove.packages("package name")
Dependency and Version Issues
Question: How to check R package dependencies?
Answer: The R package dependencies can be checked using the following command.
tools::package_dependencies("package")
Question: What if a package depends on a deprecated function?
Answer: To check if a package depends on a deprecated function, try the following:
- Update the Package (update.packages())
- Find alternatives; for example, reshape2 → tidyr
Loading and Conflict Errors
Question: Why does library()
give namespace conflict warnings?
Answer: When two packages have identical function names (e.g., filter()
in dplyr
vs. stats
), the namespace conflict occurs. This conflict can be fixed explicitly by specifying the package name. For example, to use filter()
function from dplyr
package write the command
dplyr::filter() # Explicitly specify package
One can also detach the package, for example,
detach("package:plyr", unload = TRUE)
Question: An “R package is built under a new R version.” How can such issues be resolved?
Answer: The issue of “R package is built under a new R version” can be resolved by either of the ways described below:
- Update R (recommended).
- Install an older package version:
devtools::install_version("package", version = "1.2.3")
Installation Problems
Question: Why does install.packages()
fail with “package not available”?
Answer: The possible causes are:
- Misspelled package name → Check CRAN:
available.packages()
. - R version too old → Update R (
installr::updateR()
on Windows). - CRAN mirror down → Change mirror:
chooseCRANmirror() # Pick a different server
Question: How to install from GitHub if CRAN fails?
Answer: One can install a package from GitHub if CRAN fails to install that package.
install.packages("devtools") devtools::install_github("author/package")
Note: GitHub versions may be unstable.