1

I am looking to import only a few functions from a package. Based on this issue, I can use @rawNamespace to import except.

However, what I would like to do is close to this answer. I would like to define a regular expression to only import certain functions automatically. I would like to avoid importing an entire package just for a couple of functions.

Example

#' My fancy function
#' @rawNamespace import(ggplot2, except = scale_fill_manual)
#' @export

hello_world <- function(){
  print("Hello World!")
}


In the above example, I would like to do something like:

#' My fancy function
#' @rawNamespace import(ggplot2, include = scale_*)
#' @export

hello_world <- function(){
  print("Hello World!")
}


The above example is super basic but I will actually use the imported functions somewhere else. I cannot simply use :: accessors as I am programmatically getting the functions from the namespace.

NelsonGon
  • 13,015
  • 7
  • 27
  • 57

1 Answers1

1

Based on this answer, my current workaround is:



lapply(Filter(function(x) grepl("scale_", x), getNamespaceExports("ggplot2")),
       utils::getFromNamespace, "ggplot2")

The above will allow me to import all ggplot2 scale functions while only necessitating that I specify a utils import in the Description. However, I think that this may be less ideal since perhaps it requires ggplot2 or whatever package to be on the search path.

This also is flawed because then I need to add names to the list to be able to figure out which function is which.

NelsonGon
  • 13,015
  • 7
  • 27
  • 57