[R] How to implement "zero-overhead" code re-use (a la Python, Perl, etc.) in R?

2016-09-30 Thread Kynn Jones
I'm collaborating in a long-running research project that, over the
years, has accummulated source code (written in-house) in several
languages: Python, Perl, Mathematica, MATLAB.

Recently I have started writing source code in R for this project, and
I am having trouble incorporating it into our established work flow.

Our code falls into two broad categories: "scripts" (invoked directly
by the user) and "libraries" (invoked by "client code", i.e. scripts
or other library code).

The library code lives under the top-level subdirectory ./lib of the
git-controlled project directory.  (We keep all our code, along with
the rest of the project's documents, under git control.)

Users of client programs of the code under ./lib are expected to
supply (usually via some global configuration) the appropriate library
path (.e.g PYTHONPATH=$PROJECTDIR/lib/python,
MATLABPATH=$PROJECTDIR/lib/MATLAB, etc.).

With this arrangement, code re-use is extremely simple.  For example,
by just dropping into the ./lib/python directory the file foo.py, with
content

# foo.py
def bar():
# etc.

...its bar function becomes *immediately* available, in a
namespace-safe way, to any other python code in the project, like
this:

# somescript.py

import foo

foo.bar()

I describe this form of code re-use as "zero-overhead", since it
requires only the presence of files that actually hold the code.

Under such a code re-use scheme, updates of library code from the
project's git repo are no different from updates of the project's
content in general.  All that is required is running a command like

git pull origin master

After such a command, the updated library code becomes immediately
available to client code.

Although I used Python for the example above, the picture is very
similar for the other languages we have been using up to now.

For R, however, the situation is different.  The only form of code
re-use I have found for R is through packages.  AFAICT, R packages are
not "zero-overhead": they entail a host of "meta" and derived files
(in addition to the source code files), together with
build/installation steps after each update.

I'm looking for an alternative to packages for code re-use in R, one
that better approximates the "zero-overhead" code re-use model
described earlier.

The only thing that comes to mind is as follows:

  1. a "module" is an *.R file in the directory specified a suitable
environment variable (e.g. PROJECT_R_LIB), and defining a single
"module object", which is simply a named list.  For example,

# module foo.R

foo <- list(
  bar = function (...) ... ,
  baz = function (...) ... ,
  frobozz = function (...) ... ,
  ...
  opts = list(...),
  ...
)

  2. every *.R file starts with boilerplate in the spirit of the
following (along with adequate error checking/messages, etc.):

# somescript.R

import <- function (module_name) {
  path_to_lib <- Sys.getenv("PROJECT_R_LIB")
  path_to_module <- file.path(path_to_lib, paste0(module_name, ".R"))
  source(path_to_module)
}

import("foo")
...
import("whatever")

foo$bar(...)
if (foo$opts$frobnicate) foo$frobozz(...)


This implementation is very crude (I have very little experience with
R), but I hope it at least conveys clearly what I'm after.

I would appreciate any suggestions/comments on how to implement in R
the "zero-overhead" code re-use model I described earlier.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] Fast JSON - R converter?

2009-06-13 Thread Kynn Jones
On Fri, Jun 12, 2009 at 7:16 PM, Duncan Temple Lang dun...@wald.ucdavis.edu
 wrote:


 It is not so much that rjson is implemented in R that makes it slow, just
 that it does not use vectorized operations.


 The package RJSONIO

   http://www.omegahat.org/RJSONIO


Great, I'll check it out.  Thanks!

kynn

[[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.


[R] Fast JSON - R converter?

2009-06-12 Thread Kynn Jones
Is there a *fast* converter between JSON and R?  I'm aware of the rjson
package, but it is implemented in R, and it is too slow for my purposes.
TIA!

kynn

[[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] how to insert NULLs in lists?

2009-05-23 Thread Kynn Jones
On Fri, May 22, 2009 at 7:32 PM,  markle...@verizon.net wrote:
 Hi Kynn: this oddity  is discussed in Patrick Burn's document called The R
 Inferno. I don't recall the fix so I'm not sure if below is the same as
 what his book says to do but it seems to do what you want.

Wow, I sure hit the jackpot with this link to Burn's Inferno.  It's
sooo great.  Thanks!  And to Bert, too.

KJ

__
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] how to insert NULLs in lists?

2009-05-22 Thread Kynn Jones
I'm an experienced programmer, but learning R is making me lose the little
hair I have left...

 list(NULL)
[[1]]
NULL

 length(list(NULL))
[1] 1
 x - list()
 x[[1]] - NULL
 x
list()
 length(x)
[1] 0


From the above experiment, it is clear that, although one can create a
one-element list consisting of a NULL element, one can't get the same result
by assigning NULL to the first element of an empty list.  And it gets worse:

 x - list(1, 2, 3)
 length(x)
[1] 3
 x[[2]] - NULL
 length(x)
[1] 2



I just could NOT believe my eyes!  Am I going crazy???

What I'm trying to do is so simple and straightforward: I want to be able to
append NULL to a list, and, after the appending, have the last element of
the list be NULL.  Is that so unreasonable?  How can it be done?

TIA!

Kynn

[[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.


[R] How to google for R stuff?

2009-05-20 Thread Kynn Jones
Hi!  I'm new to R programming, though I've been programming in other
languages for years.

One thing I find most frustrating about R is how difficult it is to use
Google (or any other search tool) to look for answers to my R-related
questions.  With languages with even slightly more distinctive names like
Perl, Java, Python, Matlab, OCaml, etc., usually including the name of the
language in the query is enough to ensure that the top hits are relevant.
 But this trick does not work for R, because the letter R appears by itself
in so many pages, that the chaff overwhelms the wheat, so to speak.

So I'm curious to learn what strategies R users have found to get around
this annoyance.

TIA!

KJ

[[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] How to google for R stuff?

2009-05-20 Thread Kynn Jones
Thank you all very much for the so many useful ideas and resources.
KJ

[[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.