Saturday, August 22, 2020

Blog 32: Using require function to install package in R

require function


Introduction

In this Blog we will look at how to use the require function to install a package.There are times when we need to share R scripts and run them on different systems. In such cases, the packages used in the scripts should be easily installed on different systems. For accomplishing this, we should leverage require function in ocnjuction with insall.packages().Lets look at the below example.


Installing libraries

Lets install the libraries using the following two step approach:

  • Step 1: Check if the package is already installed on the system or not using require function
  • Step 2: If the package is not installed, then install it install.packages function


package.name<-c("tidytext","dplyr","tidyr","stringr","ggplot2")

for(i in package.name){

  if(!require(i,character.only = T)){

    install.packages(i)
  }
  library(i,character.only = T)

}

In the above piece of cide, character.only = T argument means that the package name is assumed to be a character vector


Embed Shiny

Please wait...