Linking R and Julia?
Asked Answered
I

8

168

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)

Immolation answered 1/4, 2012 at 15:43 Comment(10)
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 ...Shipmate
The functions they use in the performance benchmarks seem like a pretty unusual way to use R: github.com/JuliaLang/julia/blob/master/test/perf/perf.R. It is almost like putting diesel into a Ferarri...Salutary
@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 ...Shipmate
@BenBolker Challenge posted: #9969078Immolation
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.Analyze
I have a Julia-to-R bridge roughly working (github.com/lgautier/Rif.jl). Going the other way around depends on work-still-in-progress on the Julia end.Analyze
@Analyze are you still working on Rif ? Does it still work with the latest Julia?Helmet
@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.Analyze
@Analyze 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.Japeth
@Salutary The link is broken - the new one seems to be github.com/JuliaLang/julia/blob/master/test/perf/micro/perf.RCankered
M
60

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

Messinger answered 12/8, 2015 at 17:6 Comment(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).Moitoso
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.Arther
It's Julia 1.6 now. Julia has Dict, OrderedDict, DataFrames, Named Vectors, Tuples, NamedArrays, and I'm probably forgetting some.Latish
Indeed, it's time for updates.. Thanks @LatishArther
S
65

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.

Smithereens answered 1/4, 2012 at 17:52 Comment(1)
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 timeVenditti
M
60

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

Messinger answered 12/8, 2015 at 17:6 Comment(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).Moitoso
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.Arther
It's Julia 1.6 now. Julia has Dict, OrderedDict, DataFrames, Named Vectors, Tuples, NamedArrays, and I'm probably forgetting some.Latish
Indeed, it's time for updates.. Thanks @LatishArther
R
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.

Roydd answered 23/5, 2012 at 1:6 Comment(2)
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.Immolation
Has this situation changed since you wrote this? (p.s. I'm loving Julia, thanks for your work on it!)Helmet
D
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!!

Droll answered 1/9, 2017 at 15:49 Comment(4)
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:(Devondevona
@Devondevona 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.Droll
This is needed for releasing RAM (that is allocated for Julia processes) to OS. I already have such question on stackoverflow: #61001413Devondevona
@Devondevona 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. <github.com/Non-Contradiction/JuliaCall/issues/139>Droll
I
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

Intolerance answered 2/2, 2013 at 18:10 Comment(3)
Thanks, but see comments from lgautier himself above. This is the opposite direction. I want to call Julia from within R.Immolation
+1 because in such narrow topic as Julia every info is quite informativeSupererogation
As @AriB.Friedman said, this no new info -- and we all want to call Julia from R, not the other way around.Arther
S
15

Has anyone seen this project?

https://github.com/armgong/RJulia

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

Slipcase answered 8/10, 2014 at 11:10 Comment(3)
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 github.com/armgong/RJulia/issues/10 Let's hope we get further... soonArther
Any progress? I get pretty active development notifications from their github repo so I'd imagine the issues are being ironed out...Slipcase
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.Arther
M
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.

Mcnalley answered 17/4, 2017 at 12:31 Comment(0)
S
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.

Soren answered 16/6, 2019 at 20:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.