-
-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
import from folder not working (at least on the mac) #12
Comments
I've had the same discussion with @klmr yesterday, |
In the example you’ve given, either of two things need to exist:
So |
But Does it need to contain anything or it can be just a blank file? El miércoles, 30 de abril de 2014, Konrad Rudolph notifications@github.com
Manuel Santillán |
Ah, apparently I need to clarify (meaning, write) the documentation on this point: The file can be empty. It then just serves as a “marker” that the directory is a module. It can also contain code, which is executed when the module is loaded for the first time in a session (so doing At this point the behaviour is identical to that in Python, which is why I have forgotten to document it more clearly. |
Thnx!! I've got it working now, including intra-package references. It seems a bit overkill so many init.r files BTW. Have you considered a simpler approach, like node.js's module system? http://nodejs.org/api/modules.html |
Another, related, issue I'm finding is the following: if I have the following structure: If I launch the test from the root folder
then I've got to import:
But if I launch the test from the test folder then I've got to import:
This means that imports are dependent on where is the code launched from, which seems to be a bad idea, don't you think so? Do you think this could be avoided? Does python do the same? In node.js, imports are only dependent on the relative path between the importing package and imported package, not on where the code is launched from... |
Actually, The solution here is to configure your option(import.path = '..')
import(mytest)
# … Of course that still hard-codes the knowledge that the tests will be run from within the test folder. Where that is not desired, you need to extract the path of the current file from the command line. R doesn’t make this particularly easy, unfortunately: argv <- commandArgs(trailingOnly = FALSE)
fopt <- '--file='
scriptFilename <- sub(fopt, '', argv[grep(fopt, argv)])
dirname = basename(scriptFilename) If you instead launch |
AFAIK, Python modules support relative references ('..' is not a path just a relative reference!). In python, this is called intra-package references and is a key necessity of a module system: https://docs.python.org/2/tutorial/modules.html see section 6.4.2 I really do think that a form of relative references is needed for any non-trivial use case for a module system. As a matter of fact, node.js' module system also uses dotted notation (as does python). Think for example of a project that has modules for logging, shared functions among sub-modules, and so on. |
As far as I understand, intra-package references in Python are needed (only) because, for whatever reason, external modules take precedence over local modules. Say you have a module called My R modules go the other way: |
Of course, it's your R modules, but it seems a bad idea to convolute importing a "brother" module with option(import.path...) or whatever, instead of sticking to one of the 2 mainstream approaches: A. absolute path from the project root or via a URL-y namespace -> .NET, Java and the likes go this way. This would mean that If I have the following structure:
B. Accept relative paths -> python and node.js go this way.
The key point is that it is not an edge case, but one of the most common use cases when using a module system. |
I agree with you in principle, I would simply have grouped Python with A instead of B, nothing that the relative import paths were welded on after the fact. The idea was to emulate Python’s system as far as it makes sense, but not inherit its mistakes. But you’ve actually convinced me on this point. Python’s way of doing it does make sense. |
Glad to hear that! Please let me know if I can help cheers |
I think this is now implemented. Unfortunately it’s hard to test this comprehensively, since R segfaults when trying to invoke the relevant test cases (please refer to the code for details). Note that this is not a problem of I’d therefore appreciate if you could test the current preliminary version (I haven’t created a new release for now) on your code. |
I was trying this module system, since I come from a node.js background and I think that R needs something like this really badly (congrats!)
However, I can only get to work the simplest case of same-folder modules. When I try to import modules in nested folders it doesn't work.
In my test scenario, I've got a config/log.r module I want to import. If I cd to config and import the module with import("log") it works. However, if I launch R from config's parent folder, it doesn't.
Seems that is_valid_nested considers my module is not valid. Is it mandatory to create a log folder and create a "init.r" file? Or should this just work?
The text was updated successfully, but these errors were encountered: