Hi, I am writing a function that requires a specific package to be installed. Is there a way of checking if the package is installed and returning a TRUE / FALSE result so my function can return an appropriate error message and exit the function gracefully rather than just bombing out? I'm thinking along the following lines (but want code that works), f_checkpackage <- function() { if (library(madeupname) == TRUE) { cat("package loaded OK\n") } else { cat("ERROR: package not loaded") } } f_checkpackage() -- View this message in context: http://r.789695.n4.nabble.com/checking-if-a-package-is-installed-tp2340534p2340534.html Sent from the R help mailing list archive at Nabble.com.
Hi pdb, Take a look at http://r.789695.n4.nabble.com/test-if-a-package-is-installed-td1750671.html#a1750674 HTH, Jorge On Thu, Aug 26, 2010 at 9:07 PM, pdb <> wrote:> > Hi, > > I am writing a function that requires a specific package to be installed. > > Is there a way of checking if the package is installed and returning a TRUE > / FALSE result so my function can return an appropriate error message and > exit the function gracefully rather than just bombing out? > > I'm thinking along the following lines (but want code that works), > > f_checkpackage <- function() > { > > if (library(madeupname) == TRUE) { > cat("package loaded OK\n") > } > else > { > cat("ERROR: package not loaded") > } > > } > > f_checkpackage() > -- > View this message in context: > http://r.789695.n4.nabble.com/checking-if-a-package-is-installed-tp2340534p2340534.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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]]
On Aug 26, 2010, at 9:07 PM, pdb wrote:> > Hi, > > I am writing a function that requires a specific package to be > installed. > > Is there a way of checking if the package is installed and returning > a TRUE > / FALSE result so my function can return an appropriate error > message and > exit the function gracefully rather than just bombing out? > > I'm thinking along the following lines (but want code that works), > > f_checkpackage <- function() > { > > if (library(madeupname) == TRUE) {You don't need the ==TRUE > if ( require(rms) ) {TRUE} [1] TRUE > if ( require(beanplot) ) {TRUE} else {FALSE} Loading required package: beanplot [1] FALSE Warning message: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, : there is no package called 'beanplot' According to the library/require help page library can be cajoled into returning a logical vlaue if you tell it: logical.return = TRUE . -- David.>> cat("package loaded OK\n") > } > else > { > cat("ERROR: package not loaded") > } > > } > > f_checkpackage() > -- > View this message in context: http://r.789695.n4.nabble.com/checking-if-a-package-is-installed-tp2340534p2340534.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT
require() does what you want. Run "?require" for details. require() returns 'FALSE' and gives a warning (rather than an error as 'library()' does by default) if the package does not exist. 'require' returns (invisibly) a logical indicating whether the required package is available. (You can capture the logical value by assigning it to a variable, eg. tmp <- require("pkg_name")) On 2010-8-27 9:07, pdb wrote:> Hi, > > I am writing a function that requires a specific package to be installed. > > Is there a way of checking if the package is installed and returning a TRUE > / FALSE result so my function can return an appropriate error message and > exit the function gracefully rather than just bombing out? > > I'm thinking along the following lines (but want code that works), > > f_checkpackage<- function() > { > > if (library(madeupname) == TRUE) { > cat("package loaded OK\n") > } > else > { > cat("ERROR: package not loaded") > } > > } > > f_checkpackage() >