Check if an R package is installed and if not then install it

33 views
Skip to first unread message

Ayala Allon

unread,
Sep 6, 2014, 2:11:32 AM9/6/14
to israel-r-...@googlegroups.com

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:

http://stackoverflow.com/questions/9341635/how-can-i-check-for-installed-r-packages-before-running-install-packages

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

Tal Galili

unread,
Sep 6, 2014, 2:37:17 AM9/6/14
to israel-r-...@googlegroups.com
Hi Ayala,

Not an answer to your question, but - As an alternative approach - you can use the require2 function:


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)
}


It tries to load a package, and if it fails in doing so, it will ask the user to install it for him.
(I include this function in the installr package:
)

Hope this helps.

Tal



----------------Contact Details:-------------------------------------------------------
Contact me: Tal.G...@gmail.com
Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English)
----------------------------------------------------------------------------------------------



--
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.

Ayala Allon

unread,
Sep 6, 2014, 3:41:06 AM9/6/14
to israel-r-...@googlegroups.com
Hi Tal,

Thanks!
The require2 function also loads the package after installing/ checking that I have it?

Ayala
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-group+unsub...@googlegroups.com.

Ayala Allon

unread,
Sep 6, 2014, 3:43:37 AM9/6/14
to israel-r-...@googlegroups.com
As I understand the answer to my question is yes, but I just want to make sure.
Thanks again
Ayala


--
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.

Tal Galili

unread,
Sep 6, 2014, 5:56:39 AM9/6/14
to israel-r-...@googlegroups.com
Yes. 

Sent from a smart-phone
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-g...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages