How to get the namespace exports from .onLoad()?

ghz 8months ago ⋅ 82 views

getNamespaceExports() returns an empty vector there, can I access or rebuild this info by other means ?

semi-reprex, in any package with exported objects :

.onLoad <- function(libname, pkgname) {
   print(getNamespaceExports(pkgname))
}

Answers

If getNamespaceExports() returns an empty vector, it means that the package does not export any objects explicitly via export() or exportPattern(). In this case, you won't be able to access the exported objects directly through the package namespace.

However, there are alternative ways to access or infer the exported objects:

  1. Read Documentation: Sometimes, package documentation lists exported functions, classes, or other objects. Check the documentation or the package's README file for information on available functions and objects.

  2. Explore Source Code: You can explore the source code of the package to see what functions and objects are defined. While this won't necessarily tell you what's exported, it can give you insights into the package's functionality.

  3. Use ls() on Package Namespace: You can use the ls() function on the package namespace to list all objects in the namespace, but keep in mind that this will include both exported and internal objects.

ls("package:packageName")
  1. Browse Namespace: Some integrated development environments (IDEs) allow browsing package namespaces, which can provide insights into the package's structure.

  2. Contact Package Maintainers: If you're unable to determine the exported objects using the methods above, consider reaching out to the package maintainers for clarification.

Remember that accessing non-exported objects from a package's namespace is generally discouraged, as they are subject to change without notice and may not be intended for public use. It's better to rely on documented and exported functions and objects whenever possible.