Linking R and Julia?

RJulia

R Problem Overview


Julia looks very promising for fast and syntax-sane computation (e.g. here), but I suspect it will not be anywhere near R in terms of overall statistics workflow for some time yet. So I'd like to use it where C++ is mainly used in R programs: to optimize slow portions of code. Before I invest the time in learning Julia, though, I am curious what facilities there are for embedding Julia snippets in R code.

So:

  • What facilities are out there for linking R and Julia?
  • How robust and well-thought-out are they, on a scale of zero to Rcpp?

I want to call Julia from R, just as Rcpp allows calling C++ from within R right now. I do not want to call R from Julia. (So RCall.jl would not work)

R Solutions


Solution 1 - R

I too have been looking at Julia ever since Doug Bates sent me a heads-up in January. But like @gsk3, I measure this on an "Rcpp scale" as I would like to pass rich R objects to Julia. And that does not seem to be supported at all right now.

Julia has a nice and simple C interface. So that gets us something like .C(). But as recently discussed on r-devel, you really do not want .C(), in most cases you rather want .Call() in order to pass actual SEXP variables representing real R objects. So right now I see little scope for Julia from R because of this limitation.

Maybe an indirect interface using tcp/ip to Rserve could be a first start before Julia matures a little and we get a proper C++ interface. Or we use something based on Rcpp to get from from R to C++ before we enter an intermediate layer [which someone would have to write] from which we data feed to Julia, just like the actual R API only offers a C layer. I don't know.

And the end of the day, some patience may be needed. I started to look at R around 1996 or 1997 when Fritz Leisch made the first announcements on the comp.os.linux.announce newsgroup. And R had rather limited facilities then (but the full promise of the S language, of course, si we knew we had a winner). And a few years later I was ready to make it my primary modeling language. At that time CRAN had still way less than 100 packages...

Julia may well get there. But for now I suspect many of us will get work done in R, and have just a few curious glimpses at Julia.

Solution 2 - R

The RJulia R package looks quite good now from R. R CMD check runs without warnings or errors (if julia is properly installed).

Biggest TODO in my view is to get Julia to return named lists which constitute the really basic flexible general data structure in R.

Note that Doug Bates alerted me about RCall a bi-directional interface from Julia to R (i.e., the other direction than R to Julia). Also, Doug recommended to target julia 0.4.0 rather than the current stable versions of julia.

Several more interfaces have appeared since the above was written: Now (2021-04), we've got R packages

Solution 3 - R

The Julia development plan, as I described in this answer is to allow compilation of Julia code to shared libraries, callable using the C ABI. Once this happens, it will be as easy to call Julia code from R as it is to call C/C++ code. There is, however, a fair amount of work required before this becomes possible.

Solution 4 - R

A quick update. Since this question was asked, there has been the beginnings of a Julia package that allows one to call R programs from within Julia.

More here: https://github.com/lgautier/Rif.jl

Solution 5 - R

I create an R package called JuliaCall recently, which embeds Julia in R. The package is on CRAN.

<https://cran.r-project.org/web/packages/JuliaCall/index.html>

<https://github.com/Non-Contradiction/JuliaCall>

The usage of the package is like this:

library(JuliaCall)
julia <- julia_setup()
julia_command("a = sqrt(2)"); julia_eval("a")
julia_eval("sqrt(2)")
julia_call("sqrt", 2)
julia_eval("sqrt")(2)

As you can see, you could send command strings and call Julia functions really easily.

And there are also some R packages wrapping Julia packages using JuliaCall, for example,

  • convexjlr for Disciplined Convex Programming in R using Convex.jl, which is also on CRAN.
  • ipoptjlr, an R Interface for Interior Point OPTimizer (IPOPT) using Julia package Ipopt.jl.

Welcome for any feedback on JuliaCall!!

Solution 6 - R

Has anyone seen this project?

https://github.com/armgong/RJulia

Fairly new but seems to be doing exactly what is requested!

Solution 7 - R

There is also the XRJulia package from XR family of packages aiming to eXtend R by John Chambers (one of the creators of R). It uses a bit different approach (JSON) to transfer data between Julia and R then rJulia and similar packages.

Solution 8 - R

You also might want to check out my attempt: The JuliaConnectoR R-package. The package is available from GitHub and CRAN.

It's goal is to import functions from Julia directly in R such that they can be used like R functions in R code. The return values of Julia functions are translated to R data structures, which can be used in R and also be passed back to Julia. For a further integration of Julia and R, it is also possible to call back from Julia to R by passing R functions as callback functions.

Similar to XRJulia, the JuliaConnectoR relies on TCP, but it is functionally oriented and uses an optimized custom streaming format instead of text-based JSON messages as XRJulia does. One advantage of communicating by TCP is the stability with respect to different versions of Julia and R. This is much harder to maintain with an integration at the level of C interfaces like RCall and JuliaCall do.

The package works with Julia ≥ 1.0 and a wide range of R versions.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAri B. FriedmanView Question on Stackoverflow
Solution 1 - RDirk EddelbuettelView Answer on Stackoverflow
Solution 2 - RMartin MächlerView Answer on Stackoverflow
Solution 3 - RStefanKarpinskiView Answer on Stackoverflow
Solution 4 - RaviksView Answer on Stackoverflow
Solution 5 - RConsistencyView Answer on Stackoverflow
Solution 6 - RAdamView Answer on Stackoverflow
Solution 7 - Rvh-dView Answer on Stackoverflow
Solution 8 - ReselView Answer on Stackoverflow