

This suggests that it is more efficient to use library. One final note: if you look at the source code, you can see that require calls library.

Will install “package” if it doesn’t exist, and then load it. But if you are loading it in a program that someone else is going to run, it’s a good idea to check! You can do this with, for example: if (!require(package)) install.packages('package') If you are loading a package on your own machine, you may know for sure that it is already installed. This can be very valuable for sharing code. That is, it returns (invisibly) TRUE if the package is available, and FALSE if the package is not. Unlike library, require returns a logical value. There is another difference, and it makes require(package) the preferred choice in certain situations.

I have come to prefer using library(package) for this reason. This can make troubleshooting more difficult, since the error is going to happen in, say, line 847 of your code, while the actual mistake was in line 4 when you tried to load a package that wasn’t installed. Your program will continue to run, only to crash later when you try to use a function from the library “foo.” If you use library(foo) and foo has not been installed, your program will stop with the message “Error in library(foo) : there is no package called ‘foo’.” If you use require(foo), you will get a warning, but not an error. The first one, and the most obvious, is what happens if you try to load a package that has not previously been installed. There are, though, a couple of important differences. That is, if you are loading a library that has already been installed, and you are using the command outside of a function definition, then it makes no difference if you use “ require” or “ library.” They do the same thing. The way most users will use these commands, most of the time, they are actually interchangeable. This made me wonder, are the commands interchangeable? What’s the difference, and which command should I use? Interchangeable commands. In my Linear Models class, the instructor likes to use require(package). When I was an R newbie, I was taught to load packages by using the command library(package).
