Re: [R] R equivalent of python module structure and functionality?
> Jeff Newmiller > on Wed, 12 Sep 2012 14:57:38 -0700 writes: > 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 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] R equivalent of python module structure and functionality?
It is my normal practice to install R libraries without root. Just use your own library directory instead of the system library. --- Jeff NewmillerThe . . Go Live... DCN:Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. Tom Roche wrote: > >https://stat.ethz.ch/pipermail/r-help/2012-September/323551.html >(slightly edited) >>> 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') > >>> or otherwise loaded, then called, e.g. > >>> > foo(bar='baz') > >>> ? I'm looking for the 'R equivalent' of how python supports this > >Big thanks to Trevor Davis! > >https://stat.ethz.ch/pipermail/r-help/2012-September/323559.html >> The optparse package is what you would want [to parse args for] a >> script i.e.``Rscript foo.R --bar=baz`` in a pythonic manner. > >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). > >> The other piece of the puzzle is the ``interactive()`` function >> which lets you know if you are are calling from the "R commandline". >... >> Example: > >one syntax error fixed >> ### begin foo.R # > >> # define foo function >> foo <- function(bar) { >>print(bar) >> } > >> # if not interactive we are calling from OS command line >> # parse args and call function foo >> if(!interactive()) { >>suppressPackageStartupMessages(library("optparse")) >> option_list <- list( >> make_option(c("-b", "--bar"), default="hello world") >> ) >> opt <- parse_args(OptionParser(option_list=option_list)) >> foo(bar=opt$bar) >> } > >> # end foo.R ## > >And I have modified my code @ > >https://github.com/TomRoche/GEIA_to_netCDF/commit/f982de0660b10f380183e34a0f1557a4cb1c5bb7 > >accordingly (to use `interactive()`, anyway). > >Thanks again! Tom Roche > >__ >R-help@r-project.org mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] R equivalent of python module structure and functionality?
``commandArgs`` is the function in base R that lets you access arguments passed to Rscript, does not allow any fancy parsing of options but if you are not able to install optparse it is better than nothing. - Trevor On Wed, Sep 12, 2012 at 1:32 PM, Tom Roche wrote: > > https://stat.ethz.ch/pipermail/r-help/2012-September/323551.html(slightly > edited) > >> 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') > > >> or otherwise loaded, then called, e.g. > > >> > foo(bar='baz') > > >> ? I'm looking for the 'R equivalent' of how python supports this > > Big thanks to Trevor Davis! > > https://stat.ethz.ch/pipermail/r-help/2012-September/323559.html > > The optparse package is what you would want [to parse args for] a > > script i.e.``Rscript foo.R --bar=baz`` in a pythonic manner. > > 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). > > > The other piece of the puzzle is the ``interactive()`` function > > which lets you know if you are are calling from the "R commandline". > ... > > Example: > > one syntax error fixed > > ### begin foo.R # > > > # define foo function > > foo <- function(bar) { > >print(bar) > > } > > > # if not interactive we are calling from OS command line > > # parse args and call function foo > > if(!interactive()) { > >suppressPackageStartupMessages(library("optparse")) > > option_list <- list( > > make_option(c("-b", "--bar"), default="hello world") > > ) > > opt <- parse_args(OptionParser(option_list=option_list)) > > foo(bar=opt$bar) > > } > > > # end foo.R ## > > And I have modified my code @ > > > https://github.com/TomRoche/GEIA_to_netCDF/commit/f982de0660b10f380183e34a0f1557a4cb1c5bb7 > > accordingly (to use `interactive()`, anyway). > > Thanks again! Tom Roche > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] R equivalent of python module structure and functionality?
https://stat.ethz.ch/pipermail/r-help/2012-September/323551.html (slightly edited) >> 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') >> or otherwise loaded, then called, e.g. >> > foo(bar='baz') >> ? I'm looking for the 'R equivalent' of how python supports this Big thanks to Trevor Davis! https://stat.ethz.ch/pipermail/r-help/2012-September/323559.html > The optparse package is what you would want [to parse args for] a > script i.e.``Rscript foo.R --bar=baz`` in a pythonic manner. 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). > The other piece of the puzzle is the ``interactive()`` function > which lets you know if you are are calling from the "R commandline". ... > Example: one syntax error fixed > ### begin foo.R # > # define foo function > foo <- function(bar) { >print(bar) > } > # if not interactive we are calling from OS command line > # parse args and call function foo > if(!interactive()) { >suppressPackageStartupMessages(library("optparse")) > option_list <- list( > make_option(c("-b", "--bar"), default="hello world") > ) > opt <- parse_args(OptionParser(option_list=option_list)) > foo(bar=opt$bar) > } > # end foo.R ## And I have modified my code @ https://github.com/TomRoche/GEIA_to_netCDF/commit/f982de0660b10f380183e34a0f1557a4cb1c5bb7 accordingly (to use `interactive()`, anyway). Thanks again! Tom Roche __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] R equivalent of python module structure and functionality?
>> I also wanted to point Tom to CRAN packages >> getopt >> optparse >> written specifically to support command-line argument parsing with R > >Thanks, but again, I'm not seeing how those solve the given problem(s). >Am I missing something? The optparse package, tested with Rscript but probably works with littler, provides an API similar but less powerful that what was a major python module for the parsing of optional arguments before the optparse module was deprecated in favor of the argparse module. So quite relevant for half the problem, no? It is what you would want if you wanted to use a script i.e.``Rscript foo.R --bar=baz`` in a pythonic manner. > 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') > or otherwise loaded, then called, e.g. > foo(bar='baz') The other piece of the puzzle is the ``interactive()`` function which lets you know if you are are calling from the "R commandline". So define your function foo in your script and at the end if you aren't in an interactive session parse some arguments and call your function with those arguments, otherwise if you simply source it then you should have access to that function to call as you please. Example: ### begin foo.R # # define foo function foo <- function(bar) { print(bar) } # if not interactive we are calling from OS command line # parse args and call function foo if(!interactive()) { suppressPackageStartupMessages(library("optparse")) option_list <- list( make_option(c("-b", "--bar"), default="hello world") ) opt <- parse_args(OptionParser(option_list=option_list) foo(bar=opt$bar) } # end foo.R ## So bash# Rscript foo.R --bar="baz" or > source("foo.R") > foo(bar="baz") Should both work. - Trevor [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] R equivalent of python module structure and functionality?
https://stat.ethz.ch/pipermail/r-help/2012-September/323551.html >>> 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') or otherwise loaded, then called, e.g. >>> > foo(bar='baz') >>> ? I'm looking for the 'R equivalent' of how python supports this https://stat.ethz.ch/pipermail/r-help/2012-September/323553.html >> "littler provides hash-bang (i.e. script starting with #!/some/path) >> capability for GNU R, as well as simple command-line and piping use. 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 > I also wanted to point Tom to CRAN packages > getopt > optparse > written specifically to support command-line argument parsing with R Thanks, but again, I'm not seeing how those solve the given problem(s). Am I missing something? TIA, Tom Roche __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [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 | e...@debian.org | http://dirk.eddelbuettel.com __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] R equivalent of python module structure and functionality?
On Tue, Sep 11, 2012 at 6:00 PM, Tom Roche 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 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.