Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the impact of requiring a package inside a function if the package is already loaded?

Tags:

r

require

Are there any adverse effect to including library/require statements inside of functions that will be called very frequently?

The time used seems rather negligble, but I am calling the function every few minutes and I am wondering if there is any downside to the repetitve require calls?
note that the function is just a personal util and is not being shared. ie, I am the only one using it

Incidentally, any insight as to why library is half as slow as require? I was under the impression they were synonymous.

  WithREQUIRE <- function(x) {
    require(stringr)
    str_detect(x, "hello")
  }

  WithLIBRARY <- function(x) {
    library(stringr)
    str_detect(x, "hello")
  }

  Without <- function(x) {
    str_detect(x, "hello")
  }

  x <- "goodbye"

  library(rbenchmark)
  benchmark(WithREQUIRE(x), WithLIBRARY(X), Without(x), replications=1e3, order="relative")

  #            test replications elapsed relative user.self sys.self
  #      Without(x)         1000   0.592    1.000     0.262    0.006
  #  WithREQUIRE(x)         1000   0.650    1.098     0.295    0.015
  #  WithLIBRARY(X)         1000   1.359    2.296     0.572    0.024
like image 843
Ricardo Saporta Avatar asked Mar 06 '13 21:03

Ricardo Saporta


People also ask

Which function is used to load a package?

Which function is used for loading packages? Explanation: library() function is used to load a package. library() is not useful when we are developing a package since you have to install the package first. A library is a simple directory containing installed packages.

What is a function package?

Function packages are basically several external functions and subroutines that are grouped or packaged together.

What is dependency in r?

A dependency is a code that your package needs to run. Dependencies are managed by two files. The DESCRIPTION manages dependencies at the package level; i.e. what packages needs to be installed for your package to work. R has a rich set of ways to describe different types of dependencies.


1 Answers

require checks whether the package is already loaded (on the search path)

using

loaded <- paste("package", package, sep = ":") %in% search()

and will only proceed with loading it if this is FALSE

library includes a similar test, but does a bit more stuff when this is TRUE (including creating a list of available packages.

require proceeds using a tryCatch call to library and will create a message .

So a single call to library or require when a package is not on the search path may result in library being faster

system.time(require(ggplot2))
## Loading required package: ggplot2
##   user  system elapsed 
##   0.08    0.00    0.47 
detach(package:ggplot2)
system.time(library(ggplot2))
##   user  system elapsed 
##   0.06    0.01    0.08

But, if the package is already loaded, then as you show, require is faster because it doesn't do much more than check the package is loaded.

The best solution would be to create a small package that imports stringr (or at least str_extract from stringr

like image 156
mnel Avatar answered Sep 29 '22 09:09

mnel