wahani / modules

Modules in R
https://cran.r-project.org/package=modules
Other
81 stars 3 forks source link

Search path for modules? #6

Closed banbh closed 7 years ago

banbh commented 7 years ago

When treating a script as a module, and loading via use(fileName) (as described in the README), is it possible to have a searchpath defined? The goal would be to be able to execute use("my_lib") (or use("my_lib.R")) and have a module loaded from a central location (rather than the current working directory).

wahani commented 7 years ago

Hi, thanks for using the package! I hope it helps.

What exactly do you mean by searchpath? A similar concept like the search path in R (aka search()) or that a module is loaded relative to the location of the parent module (getwd() and setwd()). Assuming you are interested in the latter, it is currently not possible. I did not implement it, because I think within the R ecosystem this behavior would be rather unexpected. Although I can see the benefits of having a system like that - as far as I know that is how Python modules work.

The idea for the usage is that the main script loads dependencies and knows where to find them. A module should assume that its root is the project root. When a module loads other modules and I worry if they can always find each other I would pick among the following options:

  1. Use the package rprojroot to determine the project root and then use absolute paths
  2. Write a package and export the modules I am interested in. This is the most reliable way, but unfortunately you will generate some overhead by having a package.
  3. Try to avoid the situation that modules load or import each other -- if a project becomes so complex that I cannot avoid it, I would go for a package. Should you have several modules, they can be grouped in a folder and loaded via use("folderName").

The forth route is to implement the feature you are looking for. However this is not easy since it was nothing I had in mind while I implemented the package. Ah, and by the way, there is another package called modules which I reference in the Readme. You can have a look if that behavior is more to your liking. I think, at least in the beginning, the development was heavily influenced by Pythons modules.

banbh commented 7 years ago

When you compared what I was asking about with Python modules you hit the nail on the head. Thank you for the detailed answer and for explaining your philosophy (and alternatives).