Hi all,
I wanted to create a few lines of code that check if an R package is installed and if not then install it. I looked in stackoverflow and I found these great options:
I picked to following option:
#will install the pROC library if you don't have it
if(!is.element('pROC', installed.packages()[,1]))
{install.packages('pROC')
}else {print("pROC library already installed")}
And this is what I wrote:
# will install package if you don't have it
packList<- list('reshape2','psych')
for (i in 1 : length(packList)){
if(!is.element(packList[i], installed.packages()[,1])){
install.packages(pacList[i])
} else {
print(paste("###",packList[i],"package is already installed ###"), quote = FALSE)
print(paste("### loading",packList[i], "###"),quote = FALSE)
library(packList[i], character.only = TRUE)
}
}
The problem is that sometime It works and sometime I get the following error:
> packList<- list('reshape2','psych')
> for (i in 1 : length(packList)){
+ if(!is.element(packList[i], installed.packages()[,1])){
+ install.packages(pacList[i])
+ } else {
+ print(paste("###",packList[i],"package is already installed ###"), quote = FALSE)
+ print(paste("### loading",packList[i], "###"),quote = FALSE)
+ library(packList[i], character.only = TRUE)
+ }
+ }
[1] ### reshape2 package is already installed ###
[1] ### loading reshape2 ###
Show Traceback
Rerun with Debug
Error in as.name(pkg) :
invalid type/length (symbol/1) in vector allocation
I tried to isolate the problem and I think the problem is in:
+ library(packList[i], character.only = TRUE)
However I still cannot figure out why I occasionally get this error and how to fix it.
Any help will be greatly appreciated.
Best,
Ayala
require2 <- function(package, ask= TRUE, ...) { | |
package <- as.character(substitute(package)) | |
if(!suppressWarnings(require(package=package, character.only = TRUE))) { | |
if(ask) { | |
install_package <- ask.user.yn.question(paste("Package ",package, | |
" is not installed. Do you want to install it now?")) | |
} else { | |
install_package <- TRUE | |
} | |
if(install_package) install.packages(pkgs=package) | |
} | |
require(package=package, character.only = TRUE) | |
} |
--
You received this message because you are subscribed to the Google Groups "Israel R User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-g...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-group+unsub...@googlegroups.com.
--
You received this message because you are subscribed to a topic in the Google Groups "Israel R User Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/israel-r-user-group/PnCtwhG8lss/unsubscribe.
To unsubscribe from this group and all its topics, send an email to israel-r-user-g...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-g...@googlegroups.com.