[R] require() is not giving TRUE / FALSE statements ?

Duncan Murdoch murdoch at stats.uwo.ca
Tue Mar 10 21:30:04 CET 2009


On 10/03/2009 4:15 PM, Wolfgang Raffelsberger wrote:
> Dear list,
> 
> ?require says :
> "... |require| is designed for use inside other functions; it returns 
> |FALSE| and gives a warning (rather than an error as |library()| does by 
> default) if the package does not exist ..."
> 
> However when I run the following code I don't get any TRUE / FALSE 
> statements (but the warning only).

You never printed the value, so you didn't see it.  (It was returned 
marked invisible, i.e. not for printing.)

Just save the value to a variable, or use it in a test, or explicitly 
print it:
 > print(require(stats))
[1] TRUE
 > print(require(doesNotExist))
Loading required package: doesNotExist
[1] FALSE
Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, 
logical.return = TRUE,  :
   there is no package called 'doesNotExist'

Duncan Murdoch

> Since I want to test from inside of a function if a given package is 
> installed (and do some specific actions like eg trying to install if the 
> answer is negative), I need the FALSE statement for use with if(). I 
> found a work-around (see below), but I wanted to bring to your attention 
> that require() doesn't give the message(s) it's supposed to return.
> 
> Wolfgang
> 
>  > require(stats)
>  > require(doesNotExist)        # example for library that's not 
> existing on my system
> Loading required package: doesNotExist
> Warning message:
> In library(package, lib.loc = lib.loc, character.only = TRUE, 
> logical.return = TRUE,  :
>   there is no package called 'doesNotExist'
> 
>  >## After all I found a work-around with :
>  >library(doesNotExist,logical.return=T)
> [1] FALSE
> Warning message:
> In library(doesNotExist, logical.return = T) :
>   there is no package called 'doesNotExist'
> 
> 
>  >## for completeness :
>  > sessionInfo()
> R version 2.8.1 (2008-12-22)
> i386-pc-mingw32
> 
> locale:
> LC_COLLATE=French_France.1252;LC_CTYPE=French_France.1252;LC_MONETARY=French_France.1252;LC_NUMERIC=C;LC_TIME=French_France.1252
> 
> attached base packages:
> [1] stats     graphics  grDevices datasets  tcltk     utils     
> methods   base    
> 
> other attached packages:
> [1] RODBC_1.2-4    svSocket_0.9-5 svIO_0.9-5     R2HTML_1.59    
> svMisc_0.9-5   svIDE_0.9-5  
> 
> loaded via a namespace (and not attached):
> [1] tools_2.8.1
> 
> 
>  
> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
> Wolfgang Raffelsberger, PhD
> Laboratoire de BioInformatique et Génomique Intégratives
> CNRS UMR7104, IGBMC 
> 1 rue Laurent Fries,  67404 Illkirch  Strasbourg,  France
> Tel (+33) 388 65 3300         Fax (+33) 388 65 3276
> wolfgang.raffelsberger (at) igbmc.fr
> 
> ______________________________________________
> 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.




More information about the R-help mailing list