-1

Avoiding potential unambiguity from namespace collisions for functions that come from libraries can be achieved by prepending the package name to the function call (e.g. dplyr::left_join, plyr::left_join etc), as explained here

How can this unambiguity be achieved for a user defined function? (i.e. one that did not come from a library/package). Obviously you cannot prepend the package name to a function if the function did not come from a package

Example

library(dplyr)
left_join <- function(x, y) { x + y }

dplyr::left_join(x,y) # Unambiguously calls left_join from dplyr
left_join(x, y) # Not clear whether from dplyr or user defined

How can user defined functions be called unambiguously?

stevec
  • 41,291
  • 27
  • 223
  • 311

1 Answers1

0

Here is a dummy function I have written. I have not rigorously tested it so it might break:

import_from_global<-function(what,...){
  func<-get(what,envir=.GlobalEnv) 
  do.call(func,list(...))
}

Test:

select<-function(){
  cat("using namespace .GlobalEnv\n") 
  print("Does nothing")
}
import_from_global("select")

Result:

using namespace .GlobalEnv
[1] "Does nothing"
NelsonGon
  • 13,015
  • 7
  • 27
  • 57