Re: [R] Install package automatically if not there?

From: Joshua Wiley <jwiley.psych_at_gmail.com>
Date: Thu, 24 Jun 2010 14:12:31 -0700

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

______________________________________________
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.
Received on Thu 24 Jun 2010 - 21:14:19 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 24 Jun 2010 - 21:40:35 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive