How to create a namespace and export a function into it?

Is there a way to declare a namespace and export a function into it so that it can be accessed with :: without creating the whole package?

The following steps are for ::: but not :::

 ns <- namespace::makeNamespace("my_namespace") assign("test",7, env=ns) my_namespace:::test # Triple colon - works. # [1] 7 my_namespace::test # Double colon - doesn't work. # Error: 'test' is not an exported object from 'namespace:my_namespace' 

Is there an alternative to assign that will make the last line work? (The goal is to model the package during its development, so other files can use it as if it were a complete package, but it can be quickly reloaded using source rather than devtools::install .)

+5
source share
2 answers

base::namespaceExport(ns, ls(ns)) seems to work (of course, you can also use a subset as a list of objects to export in the second argument). Use it as soon as you define all the objects in the namespace that you want to export:

 ns <- namespace::makeNamespace("my_namespace") assign("test", 7, env = ns) base::namespaceExport(ns, ls(ns)) my_namespace::test 

Output:

 7 
+2
source

You may find the following useful for your intended use case of package development acceleration:

http://stat.ethz.ch/R-manual/R-patched/library/utils/html/getFromNamespace.html

Description

Utilities for accessing and replacing non-exported functions in the namespace, for use in developing packages with namespaces.

+1
source

Source: https://habr.com/ru/post/1274274/


All Articles