Get function namespace

I am developing a package where I want to add a change history to an object. A package allows other packages to register functions to edit an object. I am looking for a way to record the version of the package that registered the function that was used for editing.

The question is: given the function, how do you get the package, where was it exported from? My idea is to investigate its search path, but search()

only reports the search path for the current environment and therefore not for the function I want.

Any pointers to other approaches are greatly appreciated.

The context for receiving the package is as follows:

registerFunction <- function(fun) {
  package <- getPackage(fun)  ## This is what I need
  version <- getPackageVersion(package)
  register(fun, package, version)
}

      

+3


source to share


2 answers


You can use getAnywhere

For example, if you are looking for a namespace for a function stringr

str_locate

, you can do

getAnywhere("str_locate")$where
# [1] "package:stringr"   "namespace:stringr"

      

This will work as long as it stringr

is "visible on the search path, registered as an S3 method, or namespaced but not exported."



The result is a named list and you can see what is available from getAnywhere

withnames

names(getAnywhere("str_locate"))
# [1] "name"    "objs"    "where"   "visible" "dups"  

      

+2


source


You can use:

environment(fun=someFunctionName)

      



It will return the environment of the function passed as a parameter, also specifying the namespace, that is, the package name.

+1


source







All Articles