164

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)

Frames Catherine White
  • 27,368
  • 21
  • 87
  • 137
Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235
  • 9
    My *guess* is that you'd be a very early adopter and subject to those penalties. I'd be happy to be wrong -- I'd also be happy to have you do the work and pave the way for us medium-adopters ... – Ben Bolker Apr 01 '12 at 16:07
  • 16
    The functions they use in the performance benchmarks seem like a pretty unusual way to use R: https://github.com/JuliaLang/julia/blob/master/test/perf/perf.R. It is almost like putting diesel into a Ferarri... – James Apr 01 '12 at 18:59
  • @James: that's probably because they're Julia experts, not R experts. It would be extremely interesting to see (as a matter of course) a comparison between performance of naively and expertly/idiomatically coded benchmark code -- the difference in R is bigger than in most languages, I think ... – Ben Bolker Apr 01 '12 at 21:25
  • @BenBolker Challenge posted: http://stackoverflow.com/questions/9968578/speeding-up-julias-poorly-written-r-examples – Ari B. Friedman Apr 01 '12 at 21:40
  • 2
    The question is pertinent. A C ABI for Julia seems to be emerging. There is a chance I'll be having a stab at making a Julia-to-R interface soon. – lgautier Oct 14 '12 at 12:33
  • 5
    I have a Julia-to-R bridge roughly working (https://github.com/lgautier/Rif.jl). Going the other way around depends on work-still-in-progress on the Julia end. – lgautier Dec 29 '12 at 10:51
  • @lgautier are you still working on Rif ? Does it still work with the latest Julia? – Andy Hayden Jan 31 '14 at 06:12
  • @AndyHayden I have not had much time to work on it for several months. I don't think that it is working with the current releases of Julia or R. – lgautier Jan 31 '14 at 12:26
  • 4
    @lgautier I sincerely hope you'll manage to resume your work. Hell, if there was a kickstarter page to support this, I'd sure be there. – Maxim.K Feb 03 '14 at 20:21
  • 4
    @James The link is broken - the new one seems to be https://github.com/JuliaLang/julia/blob/master/test/perf/micro/perf.R – Frank Schmitt Nov 10 '15 at 11:02

8 Answers8

64

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.

peterh
  • 11,875
  • 18
  • 85
  • 108
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • 2
    since to my knowledge there are no plans for julia to have a static compiler to allow embedding in C++ we may have to wait some time – pyCthon Aug 28 '13 at 02:30
59

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

Martin Mächler
  • 4,619
  • 27
  • 27
  • 4
    The accepted answer is outdated as of now. The best way of calling Julia from R currently is JuliaCall (see Consistency's answer below), which btw depends on RCall.jl (Julia package to call R from Julia). – Levasco Oct 13 '20 at 09:41
  • 4
    Thank you @Levasco. You are right "as of now". I acknowledge that things are changing, and what was "optimal" 5.2 years ago, no longer is currently. – Martin Mächler Oct 15 '20 at 13:52
  • It's Julia 1.6 now. Julia has ``Dict``, ``OrderedDict``, ``DataFrames``, Named ``Vectors``, ``Tuples``, ``NamedArrays``, and I'm probably forgetting some. – PatrickT Apr 22 '21 at 11:08
  • 2
    Indeed, it's time for updates.. Thanks @PatrickT – Martin Mächler Apr 26 '21 at 06:43
54

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.

Community
  • 1
  • 1
StefanKarpinski
  • 32,404
  • 10
  • 86
  • 111
  • 6
    This sounds very promising. I (and I think others) see Julia as a great replacement for how Matlab is currently used--for computation-heavy results that still require more mathematical intuition than C and its ilk provide. For that, R and Julia could be an amazing complement. Even if Julia supplants R (and I'd be fine with that, frankly), it will be at least a decade before the statistical library in Julia is anywhere near as rich, so in the meantime interfacing capabilities between R and Julia can help open-source statistical computing thrive. – Ari B. Friedman May 23 '12 at 02:11
  • 12
    Has this situation changed since you wrote this? (p.s. I'm *loving* Julia, thanks for your work on it!) – Andy Hayden Jan 31 '14 at 02:46
26

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!!

Consistency
  • 2,884
  • 15
  • 23
  • Could you help me, please - how can I exit Julia using `JuliaCall`? I run code on R and in one part of pipeline I calculate Mixed Models on Julia (using `JuliaCall`). So, I want to kill julia process after finishing of mixed models execution. Is it possible? I try `julia_command("exit()")`, but it kill R session also:( – red_quark Oct 18 '20 at 15:45
  • @red_quark There is no need to kill julia process in general. Is there any specific reason? If so, maybe you can post another question on stackoverflow or github. – Consistency Oct 19 '20 at 16:10
  • This is needed for releasing RAM (that is allocated for Julia processes) to OS. I already have such question on stackoverflow: https://stackoverflow.com/questions/61000913/close-connections-to-julia-from-under-r – red_quark Oct 19 '20 at 19:12
  • 2
    @red_quark For releasing the memory, you can release memory in julia instead of closing julia. For example, pay attention to global variables and use functions as much as possible. I recently answered a similar question on GitHub. Maybe it is helpful for you. – Consistency Oct 20 '20 at 00:10
25

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

aviks
  • 2,087
  • 18
  • 18
15

Has anyone seen this project?

https://github.com/armgong/RJulia

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

Adam
  • 284
  • 3
  • 7
  • 5
    Thanks for the hint. Indeed this would be the solution if it worked. I tried installing (using very current R 3.1.2 patched) and julia (0.4.0-dev .. updated on Dec.30, 2014 as ubuntu package). Then compilation failed and I opened github issue https://github.com/armgong/RJulia/issues/10 Let's hope we get further... soon – Martin Mächler Jan 02 '15 at 20:15
  • 2
    Any progress? I get pretty active development notifications from their github repo so I'd imagine the issues are being ironed out... – Adam Jan 21 '15 at 21:12
  • 3
    Indeed! Did not follow up here -- but on the issues page above: Major problems were all removed. I had volunteered to get the package closer to being releasable (to CRAN), namely by adding useful help pages. But unfortunately, I'm too deep in other busy matters, so this has to wait (for me) for now. – Martin Mächler Jan 23 '15 at 09:15
10

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.

vh-d
  • 421
  • 4
  • 5
9

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.

esel
  • 901
  • 9
  • 20