How should I refer to functions in imported packages?

When creating an R package, there are at least two alternatives for function references in imported packages.

Or,

  • Explicitly name the function using the double colon operator when you call it, package::function .

  • Add importFrom(package, function) to the NAMESPACE file either directly or using the #' @importFrom package function roxygen tag.

What are the advantages and disadvantages of each method?

Are there any technical differences in what each syntax achieves?

+6
source share
1 answer

Arguments for using package::function

It completely clears where the function came from.

Arguments for using @importFrom package function

It includes less typing, especially when the function is reused by your package.

Since it involves searching for a package and calling the :: function, package::function has a slight performance limit at runtime. See fooobar.com/questions/179834 / ....

In general, what is the verdict?

Both methods do the work and the arguments are not overwhelming anyway, so don't lose your sleep over this. Just choose one method and stick to it.

The policy that was adopted at my place of work is that for several commonly used packages, @importFrom roxygen tags should be used. For example, developers should know that ddply comes from plyr , or functions starting with str_ come from stringr . In this case, explicitly specifying a function is not so useful to know. For functions outside this main list (or if there is any ambiguity) :: should be used to make it clear where it came from.

+7
source

Source: https://habr.com/ru/post/971965/


All Articles