In this tutorial you will learn about the R Packages and its application with practical example.
R Packages
Package is simply a set of R functions organized in an independent, reusable unit. It usually contains set of functions for a specific purpose or utility along with the complied code and sample data. All of the R packages are stored in library directory. Every R package has its own context, thus it does not interfere with other modules.
R comes with a rich set of default packages, loaded automatically when R console is started. However, any other package other than the default need to be installed and loaded explicitly first in order to use it. Once a packages is loaded, it can be used throughout the R environment.
Check Package Directory Path
The .libPaths() function is used to show package directory path.
Example:-
1 |
.libPaths() |
Output:-
List R Packages
The library() function is used to display list of all of the installed package.
Syntax:-
1 |
library() |
Output:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
Packages in library ‘C:/Program Files/R/R-3.5.1/library’: base The R Base Package boot Bootstrap Functions (Originally by Angelo Canty for S) class Functions for Classification cluster "Finding Groups in Data": Cluster Analysis Extended Rousseeuw et al. codetools Code Analysis Tools for R compiler The R Compiler Package datasets The R Datasets Package foreign Read Data Stored by 'Minitab', 'S', 'SAS', 'SPSS', 'Stata', 'Systat', 'Weka', 'dBase', ... graphics The R Graphics Package grDevices The R Graphics Devices and Support for Colours and Fonts grid The Grid Graphics Package KernSmooth Functions for Kernel Smoothing Supporting Wand & Jones (1995) lattice Trellis Graphics for R MASS Support Functions and Datasets for Venables and Ripley's MASS Matrix Sparse and Dense Matrix Classes and Methods methods Formal Methods and Classes mgcv Mixed GAM Computation Vehicle with Automatic Smoothness Estimation nlme Linear and Nonlinear Mixed Effects Models nnet Feed-Forward Neural Networks and Multinomial Log-Linear Models parallel Support for Parallel computation in R rpart Recursive Partitioning and Regression Trees spatial Functions for Kriging and Point Pattern Analysis splines Regression Spline Functions and Classes stats The R Stats Package stats4 Statistical Functions using S4 Classes survival Survival Analysis tcltk Tcl/Tk Interface tools Tools for Package Development translations The R Translations Package utils The R Utils Package |
The search() function is used to display list of all of the packages currently loaded in the R environment.
Syntax:-
1 |
search() |
Output:-
1 2 3 |
[1] ".GlobalEnv" "package:stats" "package:graphics" [4] "package:grDevices" "package:utils" "package:datasets" [7] "package:methods" "Autoloads" "package:base" |
Installing R Package
In R, there are two ways we can install a new package.
Install using CRAN:-
CRAN is a distributed network around the world that store identical, up-to-date versions of a package code and documentation, which allows you to install a packages from nearest CRAN mirror.Use the following commands to install a R package directly from CRAN mirror –
1 2 3 |
install. packages("Package Name") # Install the package named "XML". install. packages ("XML") |
Install Manually:-
Step 1:- Download the R Package directly from the following link –
1 |
https://cran.r-project.org/web/packages/available_packages_by_name.html |
Step 2:- Save package zip file in your local system.
Step 3:- Install the package using following commands
1 2 3 |
install.packages(file_name_with_path, repos = NULL, type = "source") # Install the package named "XML" install.packages("local_path_to_package_zip_file", repos = NULL, type = "source") |
Load Package
In R, all of the default packages are loaded automatically when R console is started. However, any other package other than the default need to be installed and loaded explicitly first in order to use it. Once a packages is loaded, it can be used throughout the R environment. In R, a package can be loaded using following commands –
1 2 3 |
library("package_name", lib.loc = "path_to_library") # Load the package named "XML" install.packages("local_path_to_package_zip_file", repos = NULL, type = "source") |
Updating Package
Once a package is installed, you may frequently require to update them in order to have an latest package code. A package can be updated using update.packages command as following –
1 2 |
update.packages(ask = FALSE) # this won't ask for package updating |
Delete Package
Sometime you may require to delete any package, this can be done using the remove.packages() function as following –
1 |
remove.packages("package_name") |