lgautier / Rif.jl

Julia-to-R interface
GNU General Public License v2.0
54 stars 25 forks source link

Use an R module #3

Closed tshort closed 10 years ago

tshort commented 11 years ago

This is a feature request to make Rif work more like @stevengj's PyCall. With PyCall, one can do things like:

using PyCall
@pyimport math
math.sin(math.pi / 4) - sin(pi / 4)  # returns 0.0

It would be great if Rif could make an R module and fill it with R function calls, so that Rif.call isn't needed.

Steven's handling of keyword arguments is also nice.

lgautier commented 11 years ago

Yes, this is looking nice. I followed part this on the mailing-list (just too busy with other things to be more active at the moment).

I just had a quick glance and PyCall is using macros to define wrappers (functions) on the fly (the Python-R rpy2 interface is doing something similar, except that it is possible to define "callable" classes in Python so no need for macros).

R has a number of little surprises up its sleeve: "." (dot) is a valid character in a symbol name, as well as in a parameter's name but hopefully I'll be able to port most of the workarounds I implemented in rpy2.

diegozea commented 10 years ago

Hi Igautier!! Do you have some hints for implement this? I was thinking on ask to R for the functions and signatures in the first import, and generate a .jl file with the wrap library.

lgautier commented 10 years ago

The way I'd like to do it is pretty much the way I am doing it with rpy2:

Generating a .jl file can reduce the computer's work at import time, but will require a number of checks at import time (like checking the version of the R package). From experience with rpy2, this is not worth the trouble.

stevengj commented 10 years ago

PyCall doesn't really need macros either, the macros are just to clean up the syntax slightly. Instead of @pyimport foo as bar, you can equivalently do const bar = pywrap(pyimport("foo")).

lgautier commented 10 years ago

I see. A nice side of macros.

What it could look like in Rif is:

stats = rimport("stats")

# the original R name is var.test - a similar conversion to what is done in rpy2's `importr()` takes place 
stats.var_test(x, y)
lgautier commented 10 years ago

How do I get Julia REPL console to autocomplete after a "." (dot) ? I am guessing that I should define a method, but it is not clear to me which one.

A pointer at the exact place in PyCall implementating what let's it work with it would be fine.

stevengj commented 10 years ago

In PyCall, the pywrap function actually defines an anonymous module. So, the lookup of the fields after the . is static, not dynamic.

lgautier commented 10 years ago

I see. Pretty much like what is in rpy2 then.

I am almost done, I think, proven that someone can shed light on what is happening with the following bit inpywrap:

    eval(m, Expr(:toplevel, consts..., :(pymember(s) = getindex($(o), s)),
                 Expr(:export, exports...)))
lgautier commented 10 years ago

This is in with the latest commits, although it is in a rough form (and Rif needs a bit of work in several places).

The following is already working:

using Rif

rstats = Rif.rwrap("stats")