Skip to content
This repository has been archived by the owner on Nov 2, 2024. It is now read-only.

Is there detailed manual for Rif.jl? #38

Open
egoecho opened this issue Oct 26, 2014 · 5 comments
Open

Is there detailed manual for Rif.jl? #38

egoecho opened this issue Oct 26, 2014 · 5 comments

Comments

@egoecho
Copy link

egoecho commented Oct 26, 2014

It's really cool package, and extremely useful. The only problem is, to use this package, I have to guess the meaning of each function it provided, and do many frustrating try-error, or read the codes, since the documentary in the README.md are only illustrated the ice-burg of the whole function. Is there any detailed manual for this package?

@lgautier
Copy link
Owner

There is none yet, unfortunately, because I'd like to have the documentation in the code (the package is quite in flux, and keeping in sync a documentation separated from the code base would consume a lot of the resources available for this project - the README.md can already be a little behind at times).

While there are discussions around how to document Julia code (see for example JuliaLang/julia#5200 or JuliaLang/julia#3988), I don't think that there is a solution emerging yet (I'd be happy to be corrected if there is one).

In the meantime, I could add documentation as comments in the source or make sure the most frequently required feature are documented (even if it requires a manual sync for the time being). Just ask away.

@egoecho
Copy link
Author

egoecho commented Oct 26, 2014

I just guess Rif.@r, Rif.@R_str and Rif.Rinenv could have similar high-level function as R-magic in rpy2, isn't it? If so, could you show me a example how to use them? Can I just create a R environment, transfer data to the R-environment from Julia, run some R commands as if I do it in R, and fetch the result in the R-enviroment back to Julia?

@lgautier
Copy link
Owner

This is correct.

The catch might be that without unit-tests the macros might have drifted of usability as Julia moved along (all this started before a lot of tools for Julia existed/were reasonably stable)

I just looked at it and it is currently broken.

I am looking at it. Stay tuned.

@lgautier
Copy link
Owner

I after checking, and trying to remember that I wanted to do, I am not sure the macros ever worked the way intended.

Rif.@R / Rif.Rinenv was an attempt to blend Julia and R, and probably do things too clever for their own good: allow the use of R variables and functions in what is otherwise Julia code.

Currently, the way to get close to the ipython rmagic is Rif.R.

ipython-rmagic:

%%R
x <- rnorm(10)
m <- mean(x)

jula-Rif:

Rif.R("""
x <- rnorm(10)
m <- mean(x)
""")

Now, to access the objects in the environment one can do:

ge = Rif.getGlobalEnv()
m = ge["m"]

If you want to have this in a different environment than R's GlobalEnv, you can do the following
(note evalR(x::RExpression, env::REnvironment) is being added to the main code base so
you'll soon be able to just use Rif.evalR):

function evalR(x::Rif.RExpression, env::Rif.REnvironment)
    c_ptr = ccall(dlsym(Rif.libri, :EmbeddedR_eval),
                  Ptr{Void},
                  (Ptr{Void}, Ptr{Void}),
                  x.sexp, env.sexp)
    if c_ptr == C_NULL
        error("Error evaluating the R expression.")
    end
    return Rif._factory(c_ptr)
end

function parse_eval(r_code::ASCIIString, env::Rif.REnvironment)
   r_expression = Rif.parseR(r_code)
    return evalR(r_expression, env)
end

r_env  = Rif.R("new.env()")
res = parse_eval("""
x = rnorm(10)
m = mean(x)
""", r_env)

# get the "m" out of the execution environment
r_env["m"]

@egoecho
Copy link
Author

egoecho commented Oct 27, 2014

That's cool! Thanks!
I have another question, is there any convenient method in the package to convert a RArray, RListType and RDataArray to the corresponding Julia data type? So far I only find out converting RArray to Julia Array in such a not-so-elegant way:

N=Rif.rcall(Rif.R("nrow"),RArray_Var)[1]
M=Rif.rcall(Rif.R("ncol"),RArray_Var)[1]
reshape([RArray_Var...],N,M)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants