Discussion: Function Namespace for Package Development, pkg::fn or @import pkg fn?

Hello RStudio Community!

My colleagues and I have started to build-out multiple R packages for our company. We have built all of our packages thus far using the :: operator to access functions from external packages we use. We are starting to explore the golem package to turn our shiny apps into R packages. Generally, when we make shiny apps we follow the tradition of putting library(...) on top. Since we are in package land now, we have been discussing what route makes sense for long term development. Should we switch to using @import/@importFrom within roxygen, or do we have our developers continue using ::? @import does make the script cleaner, however it can be hard to track which function comes from where.

Karl Broman wrote a minimal tutorial about R packages where he mentions (under the NAMESPACE file header): "And probably it’s best to skip the whole @import and @importFrom technique and just use the :: operator, particularly for clarity: jsonlite::unbox( ) makes it clear that unbox is not part of the present package but is part of jsonlite." This currently makes sense to us and that is why we use the :: while developing in packages.

We are a smaller group of developers and are wanting to bring in others into this discussion. What do you do for production-ready packages? Is there a standard we are unaware of? Please share any stories and/or reasons why you chose one over the other.

Thanks!

2 Likes

I also tend to use pkg:: to handle namespace issues in a package, mainly for the reasons you cited. Another benefit is that you can easily search your entire package to get a sense of how much you rely on a specific dependency.

The main exception I make is for readability. If a function name is short and it is used often, I'll import it. The main examples from my work that I avoid is dplyr::%>% (or magrittr::%>%) and rlang::!!:

#' @importFrom dplyr "%>%"
#' @importFrom rlang "!!"

I think ggplot2 and shiny are the toughest call, and it'd have to come down to the personal preference of you and your colleagues. A good piece of advice from the vignette Using ggplot2 in packages is that if you are going to import functions from an external dependency, only explicitly import the specific functions you are using with @importFrom.

3 Likes