[R] Using require() vs. library()

Brian Diggs diggsb at ohsu.edu
Thu Aug 18 20:56:32 CEST 2011


On 8/17/2011 11:13 AM, Uwe Ligges wrote:
> Actually require() is a wrapper around library() with more error
> handling to be used inside other functions. Just type require(), you can
> read the few lines of code quickly.

I think the unstated corollary is that library() is preferred when not 
inside a function, but within a file that might be sourced.  That is 
because if the package can not be loaded, execution will halt at that 
point.  If require were used, then execution would continue, almost 
certainly unsuccessfully.  Compare two files:

library("nonexistant")
print("done")

versus

require("nonexistant")
print("done")

Sourcing the first gives:

Error in library("nonexistant") :
   there is no package called 'nonexistant'

Sourcing the second gives:

Loading required package: nonexistant
[1] "done"
Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, 
logical.return = TRUE,  :
   there is no package called 'nonexistant'

Maybe put another way, if you aren't going to do anything with the 
return value of require, then you are probably better off using library 
so it fails sooner.

> Uwe Ligges
>
>
>
> On 17.08.2011 19:57, Nordlund, Dan (DSHS/RDA) wrote:
>>> -----Original Message-----
>>> From: istazahn at gmail.com [mailto:istazahn at gmail.com] On Behalf Of Ista
>>> Zahn
>>> Sent: Wednesday, August 17, 2011 10:12 AM
>>> To: Nordlund, Dan (DSHS/RDA)
>>> Cc: r-help at r-project.org
>>> Subject: Re: [R] Using require() vs. library()
>>>
>>> Hi Dan,
>>> Is there something you would like to know that is not covered by
>>> help(library) ?
>>>
>>> Best,
>>> Ista
>>>
>>> On Wed, Aug 17, 2011 at 12:40 PM, Nordlund, Dan (DSHS/RDA)
>>> <NordlDJ at dshs.wa.gov> wrote:
>>>> A recent post prompts me to ask this question. Is there any reason
>>> to prefer using library() over require()? I tend to use require()
>>> instead of library() to load packages, but I wonder if there are
>>> situations where it would be better to use library().
>>>
>>
>> Well, I guess when I read that "require is designed for use inside
>> other functions..." I wasn't sure if that meant there might be times
>> when it would be better to use library when not inside other
>> functions. But maybe it was more generally a question about style,
>> prompted by a post responding to the common confusion between the
>> terms 'package' and 'library' amongst those new to R. To me, it always
>> seemed more natural type require(my.package) than library(my.package).
>> I just wanted to make sure I wasn't missing something that might make
>> me regret that choice.
>>
>> Dan
>>
>> Daniel J. Nordlund
>> Washington State Department of Social and Health Services
>> Planning, Performance, and Accountability
>> Research and Data Analysis Division
>> Olympia, WA 98504-5204
>>
>>
>> ______________________________________________
>> 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.
>


-- 
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University



More information about the R-help mailing list