0

When making your own package for R, one often wants to make use of functions from a different package. Maybe it's a plotting library like ggplot2, dplyr, or some niche function.

However, when making a function that depends on functions in other packages, what is the appropriate way to call them? In particular, I am looking for examples of when to use

myFunction <- function(x) {
example_package::function(x)
}

or

require(example_package)

myFunction <- function(x) {
function(x)
}

When should I use one over the other?

mhovd
  • 3,724
  • 2
  • 21
  • 47

2 Answers2

4

If you're actually creating an R package (as opposed to a script to source, R Project, or other method), you should NEVER use library() or require(). This is not an alternative to using package::function(). You are essentially choosing between package::function() and function(), which as highlighted by @Bernhard, explicitly calling the package ensures consistency if there are conflicting names in two or more packages.

Rather than require(package), you need to worry about properly defining your DESCRIPTION and NAMESPACE files. There's many posts about that on SO and elsewhere, so won't go into details, see here for example.

Using package::function() can help with above if you are using roxygen2 to generate your package documentation (it will automatically generate a proper NAMESPACE file.

caldwellst
  • 5,719
  • 6
  • 22
0

The douple-colon variant :: has a clear advantage in the rare situations, when the same function name is used by two packages. There is a function psych::alpha to calculate Cronbach's alpha as a measure of internal consistency and a function scales::alpha to modify color transparency. There are not that many examples but then again, there are examples. dplyr even masks functions from the stats and base package! (And the tidyverse is continuing to produce more and more entries in our namespaces. Should you use dyplr you do not know, if the base function you use today will be masked by a future version of dplyr thus leading to an unexpected runtime problem of your package in the future.)

All of that is no problem if you use the :: variant. All of that is not a problem if in your package the last package opened is the one you mean.

The require (or library) variant leads to overall shorter code and it is obvious, at what time and place in the code the problem of a not-available package will lead to an error and thus become visible.

In general, both work well and you are free to choose, which of these admittedly small differences appears more important to you.

Bernhard
  • 4,272
  • 1
  • 13
  • 23