Skip to content

R equivalent of python module structure and functionality?

9 messages · James, Dirk Eddelbuettel, Tom Roche +3 more

#
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)
? 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
) 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.,
How to do this in R?

TIA, Tom Roche <Tom_Roche at pobox.com>
#
On Tue, Sep 11, 2012 at 6:00 PM, Tom Roche <Tom_Roche at pobox.com> 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


James
#
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
#
https://stat.ethz.ch/pipermail/r-help/2012-September/323551.html
or otherwise loaded, then called, e.g.
https://stat.ethz.ch/pipermail/r-help/2012-September/323553.html
Apparently so, but I don't see how that jointly satisfies the usecases
above. (Also, I don't see what littler provides that Rscript doesn't--
both do hash-bang--but that's probably orthogonal to this question.)

https://stat.ethz.ch/pipermail/r-help/2012-September/323554.html
Thanks, but again, I'm not seeing how those solve the given problem(s).
Am I missing something?

TIA, Tom Roche <Tom_Roche at pobox.com>
#
https://stat.ethz.ch/pipermail/r-help/2012-September/323551.html (slightly edited)
Big thanks to Trevor Davis!

https://stat.ethz.ch/pipermail/r-help/2012-September/323559.html
Unfortunately, the R on the cluster on which I'm working currently
lacks that package, and I lack root on that cluster. But your example
(below) works on my box (where I'm root).
...
one syntax error fixed
And I have modified my code @

https://github.com/TomRoche/GEIA_to_netCDF/commit/f982de0660b10f380183e34a0f1557a4cb1c5bb7

accordingly (to use `interactive()`, anyway).

Thanks again! Tom Roche <Tom_Roche at pobox.com>
#
It is my normal practice to install R libraries without root. Just use your own library directory instead of the system library.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.
Tom Roche <Tom_Roche at pobox.com> wrote:

            
1 day later
#
> It is my normal practice to install R libraries without
    > root. Just use your own library directory instead of the
    > system library.
    > ---------------------------------------------------------------------------
 
    > Sent from my phone. Please excuse my brevity.

well, "libraries" is even longer than "packages", so please,
increasing your brevity, do use correct language.

install.packages("fortunes")
require("fortunes")

fortune("It's a package")

Martin