[R] checking if a package is installed

Dejian Zhao zhaodj at ioz.ac.cn
Fri Aug 27 08:22:36 CEST 2010


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



More information about the R-help mailing list