Reputation: 117
I have wrote a function for the packages which I have already installed. Within that I just want to write a statement where a new package needs to be installed automatically, once we call it. I want to install ROCR package using this function below.
libraries <- function()
list.of.packages <- c("caTools", "caret", "glmnet","rpart","rpart.plot","randomForest","rattle",
"e1071")
new.packs<- if(list.of.packages[!list.of.packages %in% installed.packages() [,"Package"])]{
install.packages("new.packs")
library(new.packs)
}else {
print("All packages installed")
} }
Upvotes: 0
Views: 549
Reputation: 3656
library pacman does this for you.
e.g.
pacman::p_load(data.table, lubridate)
loads the libraries and installs them if they are not available.
Upvotes: 3
Reputation: 1664
How about this general approach:
if(!"caret" %in% installed.packages()) install.packages("caret")
Can be adjusted to your needs, as in getting vector of not yet installed packages and then passing it to install.packages
:
list.of.packages <- c("caTools", "caret", "glmnet","rpart","rpart.plot","randomForest","rattle",
"e1071")
if(length(which(!list.of.packages %in% installed.packages()))){
install.packages(list.of.packages[!list.of.packages %in% installed.packages()])
}
Upvotes: 1