Tom Roche
2012-Sep-11 23:00 UTC
[R] R equivalent of python module structure and functionality?
summary: how to structure an R file such that it can be both 1. used as a script via, e.g., (from OS commandline) $ Rscript foo.r bar=baz 2. imported and called as a function via, e.g. (from R commandline)> source('./foo.r) > foo(bar='baz')? I'm looking for the 'R equivalent' of how python supports this usecase. details: As discussed in the thread beginning https://stat.ethz.ch/pipermail/r-help/2012-September/323255.html I have a script https://github.com/TomRoche/GEIA_to_netCDF/blob/master/netCDF.stats.to.stdout.r that takes named arguments without undue pain. I would also like to be able to call it as a function from other scripts. How to do that in R? In case that's not specific enough :-) I know how to structure files/ modules in python like http://python.net/~goodger/projects/pycon/2007/idiomatic/cmdline.py (i.e., generically, http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#module-structure> """module docstring""" > > # imports > # constants > # exception classes > # interface functions > # classes > # internal functions & classes > > def main(...): > ... > > if __name__ == '__main__': > status = main() > sys.exit(status)) so that the file/module is both 1. callable from the OS commandline via, e.g., $ /path/to/cmdline.py 2. importable without mere import causing execution of the script's functionality, e.g.,>>> sys.path.append('/path/to') >>> from cmdline import * >>> process_command_line(...)How to do this in R? TIA, Tom Roche <Tom_Roche at pobox.com>
J Toll
2012-Sep-11 23:47 UTC
[R] R equivalent of python module structure and functionality?
On Tue, Sep 11, 2012 at 6:00 PM, Tom Roche <Tom_Roche at pobox.com> wrote:> > summary: how to structure an R file such that it can be both > > 1. used as a script via, e.g., (from OS commandline) > > $ Rscript foo.r bar=baz > > 2. imported and called as a function via, e.g. (from R commandline) > >> source('./foo.r) >> foo(bar='baz') > > ? I'm looking for the 'R equivalent' of how python supports this > usecase. > > details: > > As discussed in the thread beginning > > https://stat.ethz.ch/pipermail/r-help/2012-September/323255.html > > I have a script > > https://github.com/TomRoche/GEIA_to_netCDF/blob/master/netCDF.stats.to.stdout.r > > that takes named arguments without undue pain. I would also like to be > able to call it as a function from other scripts. How to do that in R? > In case that's not specific enough :-) I know how to structure files/ > modules in python like > > http://python.net/~goodger/projects/pycon/2007/idiomatic/cmdline.py > > (i.e., generically, > > http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#module-structure >> """module docstring""" >> >> # imports >> # constants >> # exception classes >> # interface functions >> # classes >> # internal functions & classes >> >> def main(...): >> ... >> >> if __name__ == '__main__': >> status = main() >> sys.exit(status) > > ) so that the file/module is both > > 1. callable from the OS commandline via, e.g., > > $ /path/to/cmdline.py > > 2. importable without mere import causing execution of the script's > functionality, e.g., > >>>> sys.path.append('/path/to') >>>> from cmdline import * >>>> process_command_line(...) > > How to do this in R?Maybe take a look at littler. "littler provides hash-bang (i.e. script starting with #!/some/path) capability for GNU R, as well as simple command-line and piping use." http://dirk.eddelbuettel.com/code/littler.html James
Dirk Eddelbuettel
2012-Sep-12 00:39 UTC
[R] R equivalent of python module structure and functionality?
On 11 September 2012 at 18:47, J Toll wrote: | Maybe take a look at littler. | | "littler provides hash-bang (i.e. script starting with #!/some/path) | capability for GNU R, as well as simple command-line and piping use." | | http://dirk.eddelbuettel.com/code/littler.html Thanks for the plug. I also wanted to point Tom to CRAN packages getopt optparse written specifically to support command-line argument parsing with R scripts (ie via Rscript and littler). Dirk -- Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
Apparently Analagous Threads
- [ncdf4] error converting GEIA data to netCDF
- [newbie] failure to plot a RasterLayer with raster::plot or fields::image.plot
- [Rscript] difficulty passing named arguments from commandline
- [LLVMdev] PyPy sprint announcement: PyCon 2006, Texas, Feb 27st - March 2nd
- [LLVMdev] OT: Python on LLVM