Open In App

P_load() function in R

Last Updated : 02 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

P_load() function in R Programming Language is kind of a validation function that checks whether the particular package is installed. If not, it will attempt to install that package from CRAN or any other repository in the Pacman repo list.

Syntax: p_load(char, install , update m, character.only)

Parameters:

  • char – denotes the vector of characters containing packages to load
  • install – (bool) if TRUE will try to install a package that is not found in the library. If FALSE it doesn’t install
  • update –  (bool) if TRUE will try to update all outdated packages
  • character.only – (bool) If TRUE then the function will only accept a single input of character vector containing the names of packages to load.

Let us see a practical example of how to use the p_load function. First, let us load the p_load function from the pacman library and then use the p_load function to check if the given package exists.

Here, we have used the p_load function to check if the requests package is already present in the library, here we must also note that we have made INSTALL as FALSE meaning if the package is not present it will not try to install the same.

R




# load the pacman library
if(!require('pacman')) {
  install.packages('pacman')
  library('pacman')
}
  
# loading the p_load function
p_load(requests, install = FALSE)


Output:

Since this package is not available, and install is false, the output is as follows.

Output

Now, let us try the same example with install TRUE.

R




# load the pacman library
if(!require('pacman')) {
  install.packages('pacman')
  library('pacman')
}
  
# loading the p_load function
p_load(requests, install = TRUE)


Output:

Since this package is not available, and install is false, p_load tries and installs the package

Output

Now, let us use both the parameters install and update as TRUE ensuring that the packages are installed  and updated.

R




# load the pacman library
if(!require('pacman')) {
  install.packages('pacman')
  library('pacman')
}
  
# loading the p_load function
p_load(request, install = TRUE, update = TRUE)


Output:

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads