Introduction to Packages

Author

Dr. Mohammad Nasir Abdullah

Introduction to Packages

A package in R is a collection of functions, sample data, and documentation bundled together. By using packages, you can leverage the work of others to perform complex tasks with just a few lines of code.

Why Use Packages?

Enhanced Functionality: Packages provide additional functions to perform a wide variety of tasks.

Efficiency: Save time and effort by using pre-written and tested code.

Community Support: Benefit from the extensive and vibrant R community.

Installing packages

You can install packages directly from CRAN (Comprehensive R Archive Network), or other repositories, and also from local files.

#installing the 'dplyr' package from CRAN 
install.packages("dplyr")

Loading packages

After installing a package, you need to load it into the R environment to use its functions.

#loading the 'dplyr' package 
library(dplyr)

Using package functions

After loading a package, you can use its functions by calling them like any other function in R.

#using the 'filter' function from 'dplyr' to filter rows in a data frame.  
dplyr::filter(mtcars, mpg > 20)

To see list of functions available in a package

ls(getNamespace("dplyr"))

To see the documentation of the package

help(package="dplyr")

Install Packages from GitHub

To install R packages from GitHub, you can use the devtools packages, which allows you to install R packages directly from GitHub repositories.

Step 1: Install the devtools package

install.packages("devtools")

Step 2: Load the devtools package

library(devtools)

Step 3: Install the package from GitHub

Now, you can install the package from GitHub using the install_github function. To install the nasirds package from karraz/nasirds repository: https://github.com/karraz/nasirds

install_github("karraz/nasirds")

Step 4: Load the installed package

once the package is installed, you can load it as usual

library(nasirds)
ls(getNamespace("nasirds")) #to see the available function
[1] "nsummary"
?nsummary #documentation of the function
nsummary(mtcars$mpg) #use the function

          Numerical Statistics
Mean                 20.090625
Std_Dev               6.026948
Minimum              10.400000
Maximum              33.900000
Range                23.500000
Quartile1            15.425000
Median               19.200000
Quartile3            22.800000
IQR                   7.375000