rticulate / import

An Import Mechanism For R
https://import.rticulate.org
Other
222 stars 14 forks source link

(Enhancement) Register module as a namespace #94

Open J-Moravec opened 3 weeks ago

J-Moravec commented 3 weeks ago

In R, the formal organisational units of code are packages. While there is a lot of tooling to make creating packages easier, it still takes quite a lot of work, especially where the code is specific to analysis and changes often. For this reason, import as well as many other packages were created to help organise the often chaotic source()ing code.

Namespaces are the organisational unit of packages, their support is not established past that. When namespaces were established, there were talks about packages having multiple namespaces, or namespace being shared by multiple packages, but this support was never implemented. In (some) other languages, namespaces are the organisational unit of code, allowing for independent modules as well as a better sense of dependency management.

Currently, import as well as many other similar packages do not support namespaces, instead, they typically import the code into environments allowing the $ semantics when calling specific function or object (i.e., env$fun()). With packages, one would use :: instead (i.e., pkg::fun()) when wanting to be specific. While minor, this creates a difference between code organised into a package versus a code organised into modules. (in fact, all one needs to do this is to specify the environment in source() or sys.source()).

With the use of Namespaces, the code could be further organised and refined, distinguishing explicitly between private and exported function as well as to use the module::fun semantics calling functions. This would make future re-use of code to form a package much easier since all the calls could be explicit.

Sadly, currently there is no way to do that in base, but see the following StackOverflow question:

https://stackoverflow.com/questions/47941426/how-to-create-a-namespace-and-export-a-function-into-it

The used package namespace is archived, but after inspecting the code, all it does is exports/copy/glue some base R code.

torfason commented 2 weeks ago

Sounds interesting, but also a big task. I do worry a bit about implementing the stuff that was implemented in the namespace, given that that package has been archived due to issues that changed in R and were not fixed. It seems like it would add considerably to the "compatibility surface" and could make it harder to keep import on CRAN in the long run.

Could the namespace package be resurrected, or a new (slimmer?) namespace2 package be created? I would feel better about calling such a package than to add a lot of code for managing namespaces into import.