Hi fans, is it possible for a script to check if a library has been installed? I want to automatically install it if it is missing to avoid scripts to crash when running on a new machine... Ralf
On Jun 24, 2010, at 3:25 PM, Ralf B wrote:> Hi fans, > > is it possible for a script to check if a library has been installed? > I want to automatically install it if it is missing to avoid scripts > to crash when running on a new machine...Puzzled. When you went to the help page for install.packages, what did you think the link to installed.packages was offering? -- David.
Something like: if (!require(pkg)){ install.packages(pkg) } Jason -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Ralf B Sent: Thursday, June 24, 2010 12:26 PM To: r-help at r-project.org Subject: [R] Install package automatically if not there? Hi fans, is it possible for a script to check if a library has been installed? I want to automatically install it if it is missing to avoid scripts to crash when running on a new machine... Ralf ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
You mean if a "package" has been installed?! (big difference) ?installed.packages or ?.packages with all.available = TRUE ?install.packages Bert Gunter Genentech Nonclinical Biostatistics -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Ralf B Sent: Thursday, June 24, 2010 12:26 PM To: r-help at r-project.org Subject: [R] Install package automatically if not there? Hi fans, is it possible for a script to check if a library has been installed? I want to automatically install it if it is missing to avoid scripts to crash when running on a new machine... Ralf ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Hi Ralf, Ralf B <ralf.bierig at gmail.com> writes:> Hi fans, > > is it possible for a script to check if a library has been installed? > I want to automatically install it if it is missing to avoid scripts > to crash when running on a new machine...You could do something like if ("somepackage" %in% row.names(installed.packages()) == FALSE) install.packages("somepackage") -Ista> > Ralf > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
require(pkg) || install.packages(pkg) Cheers!! Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --- On Thu, 6/24/10, Ralf B <ralf.bierig@gmail.com> wrote: From: Ralf B <ralf.bierig@gmail.com> Subject: [R] Install package automatically if not there? To: "r-help@r-project.org" <r-help@r-project.org> Date: Thursday, June 24, 2010, 9:25 PM Hi fans, is it possible for a script to check if a library has been installed? I want to automatically install it if it is missing to avoid scripts to crash when running on a new machine... Ralf ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]]
Hello Ralf, This is a little function that you may find helpful. If the package is already installed, it just loads it, otherwise it updates the existing packages and then installs the required package. As in require(), 'x' does not need to be quoted. load.fun <- function(x) { x <- as.character(substitute(x)) if(isTRUE(x %in% .packages(all.available=TRUE))) { eval(parse(text=paste("require(", x, ")", sep=""))) } else { update.packages() # recommended before installing so that dependencies are the latest version eval(parse(text=paste("install.packages('", x, "')", sep=""))) } } HTH, Josh On Thu, Jun 24, 2010 at 12:25 PM, Ralf B <ralf.bierig at gmail.com> wrote:> Hi fans, > > is it possible for a script to check if a library has been installed? > I want to automatically install it if it is missing to avoid scripts > to crash when running on a new machine... > > Ralf > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Joshua Wiley Ph.D. Student Health Psychology University of California, Los Angeles
Nice. Very nice. -- David. On Jun 24, 2010, at 4:32 PM, Albert-Jan Roskam wrote:> require(pkg) || install.packages(pkg) > > Cheers!! > > Albert-Jan > > > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > All right, but apart from the sanitation, the medicine, education, > wine, public order, irrigation, roads, a fresh water system, and > public health, what have the Romans ever done for us? > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > --- On Thu, 6/24/10, Ralf B <ralf.bierig at gmail.com> wrote: > > From: Ralf B <ralf.bierig at gmail.com> > Subject: [R] Install package automatically if not there? > To: "r-help at r-project.org" <r-help at r-project.org> > Date: Thursday, June 24, 2010, 9:25 PM > > Hi fans, > > is it possible for a script to check if a library has been installed? > I want to automatically install it if it is missing to avoid scripts > to crash when running on a new machine... > > Ralf > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Hello Ralf, Glad it works for you. As far as avoiding any prompting if packages are out-of-date; I am not sure. It honestly seems like a risky idea to me to have old packages being overwritten without a user knowing. However, I added a few lines of code here to set the CRAN mirror, and revert it back to whatever it was when the function ends to make it as inobtrusive as possible. load.fun <- function(x) { old.repos <- getOption("repos") on.exit(options(repos = old.repos)) #this resets the repos option when the function exits new.repos <- old.repos new.repos["CRAN"] <- "http://cran.stat.ucla.edu" #set your favorite CRAN Mirror here options(repos = new.repos) x <- as.character(substitute(x)) if(isTRUE(x %in% .packages(all.available=TRUE))) { eval(parse(text=paste("require(", x, ")", sep=""))) } else { update.packages() # recommended before installing so that dependencies are the latest version eval(parse(text=paste("install.packages('", x, "')", sep=""))) eval(parse(text=paste("require(", x, ")", sep=""))) } } Best regards, Josh On Thu, Jun 24, 2010 at 3:34 PM, Ralf B <ralf.bierig at gmail.com> wrote:> Thanks, works great. > > By the way, is there a say to even go further, and avoid prompting > (e.g. picking a particiular server by default and updating without > further prompt) ? ?I could understand if such a feature does not exist for > security reasons... > > Thanks again, > Ralf > > On Thu, Jun 24, 2010 at 5:12 PM, Joshua Wiley <jwiley.psych at gmail.com> wrote: >> On Thu, Jun 24, 2010 at 1:51 PM, Joshua Wiley <jwiley.psych at gmail.com> wrote: >>> Hello Ralf, >>> >>> This is a little function that you may find helpful. ?If the package >>> is already installed, it just loads it, otherwise it updates the >>> existing packages and then installs the required package. ?As in >>> require(), 'x' does not need to be quoted. >>> >>> load.fun <- function(x) { >>> ?x <- as.character(substitute(x)) >>> ?if(isTRUE(x %in% .packages(all.available=TRUE))) { >>> ? ?eval(parse(text=paste("require(", x, ")", sep=""))) >>> ?} else { >>> ? ?update.packages() # recommended before installing so that >>> dependencies are the latest version >>> ? ?eval(parse(text=paste("install.packages('", x, "')", sep=""))) >>> ?} >>> } >> >> I miscopied the last line; it should be.... >> >> ########### >> load.fun <- function(x) { >> ?x <- as.character(substitute(x)) >> ?if(isTRUE(x %in% .packages(all.available=TRUE))) { >> ? ?eval(parse(text=paste("require(", x, ")", sep=""))) >> ?} else { >> ? ?update.packages() # recommended before installing so that >> dependencies are the latest version >> ? ?eval(parse(text=paste("install.packages('", x, "')", sep=""))) >> ? ?eval(parse(text=paste("require(", x, ")", sep=""))) >> ?} >> } >> ########### >> >>> >>> HTH, >>> >>> Josh >>> >>> On Thu, Jun 24, 2010 at 12:25 PM, Ralf B <ralf.bierig at gmail.com> wrote: >>>> Hi fans, >>>> >>>> is it possible for a script to check if a library has been installed? >>>> I want to automatically install it if it is missing to avoid scripts >>>> to crash when running on a new machine... >>>> >>>> Ralf >>>> >>>> ______________________________________________ >>>> R-help at r-project.org mailing list >>>> https://stat.ethz.ch/mailman/listinfo/r-help >>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >>>> and provide commented, minimal, self-contained, reproducible code. >>>> >>> >>> >>> >>> -- >>> Joshua Wiley >>> Ph.D. Student >>> Health Psychology >>> University of California, Los Angeles >>> >> >> >> >> -- >> Joshua Wiley >> Ph.D. Student >> Health Psychology >> University of California, Los Angeles >> >-- Joshua Wiley Ph.D. Student Health Psychology University of California, Los Angeles