Now that JuliaC.jl is released, I want to try something I’ve always wanted: being able to pass a matrix from R into Julia, have it do something, then returning a scalar/vector/matrix back into R. I’m thinking of compiling 2-3 different julia apps (one for each OS) and have them be part of my Rpackage directly.
Has anyone done this before? Just wanted to see if there’s some examples before I get into the weeds.
For R you probably want to consider the .Call interface from R (see the R manual “Writing R Extensions” for details). In .Call all the arguments to a C-callable function in a shared library are pointers to symbolic expressions (SEXPREC) which is the basic internal object type in R. The return value from such a function is also a pointer (SEXP) to a SEXPREC.
The code for boxing/unboxing SEXPRECs from the Julia side is currently part of the RCall package. If using JuliaC to created shared libraries of R callable functions becomes popular it may be worthwhile separating the boxing/unboxing code into its own Julia package.
Oh if only there is a legendary figure who knew both the dark magic of R’s internals and the light of Julia’s macros! If such a person existed… surely they will descend from the mountains and show us mortals how to tame the SEXPs and make them obey Julia’s will
Even though I was part of R Core for twenty years it was when I started work on RCall.jl (I just started it, the majority of the development and enhancement was done by Simon and Randy) that I really began to understand the internals of R, because I could use Julia to pick apart those structures. One of the beautiful aspects of Julia is that you have so much power and flexibility available to you in a REPL.
This example uses the more basic .C interface from R, not the .Call or .External interface. I think it should be possible to extract the code defining methods for rcopy and sexp from the RCall.jl package into a separate package, say RObjects.jl, so that that code can be used in both RCall.jl and in code that will be compiled to a shared library to be called from R.
By the way, please comment in that issue if there are better ways of calling juliac, etc. for this usage. As I said, this is a proof-of-concept at this stage, not a demonstration that I know what I’m doing.
Note you can already call Julia from R (and e.g. Python), independently of juliac (you can compile in many ways or just skip doing that; C in juliac means compiler not the C language, though in some sense it will also compile Julia code to “C API” usable code):
It seem you mean you want Julia libraries, to call from e.g. an R app? JuliaC can compile apps, yes, but also libraries I understand, and you want the latter? You will not call or “use” juliac from the other languages (so I would clarify the title here), only using once to make a dynamic-link library, and then you can call that library from your other language as easily as calling C.
You don’t need juliac, just to call, e.g. pass a matrix, it’s just if you want a smaller binary (dropping most of the Julia runtime). It seemingly shouldn’t be too hard just even easier with juliacall.
Note the juliacall for Python is very smooth, will download Julia for you, but the juliacall for R is I think more manual (still, could be improved), and note this:
You can use it but you will avoid that issue by using juliac yes. If you just want to use Julia from other languages, as easily as possible (and don’t care from which; first), I would start with from Python, and then you could use juliac after that is working. [You can also use Julia as the main language, with or without compiling a julia app (using e.g. juliac, not the only option to compile!), including to call the other languages, one or even both/any, but didn’t seem what you were after.]
You can have some C library callable from R, I assume easy, you can also have the same library callable from Python, and also if you compile Julia code into a library, it will look more or less like a C library or dynamic link library, so as easy. What I’m not 100% sure if is what are the redundant steps you need for Python vs R, i.e. if some needed, or even if some, or all, can be automated, to make easier to call from both of the languages. I think R is usually calling C++ code, but more common to call C code from Python, then will be more different I think, but I think you can just as easily call C (then also Julia) from R, since C is a (non-strict) subset of C++.
Great! I see a comment from 1 hour ago, and you opened that issue on Feb 27. RCall.jl is well of course for calling R, and its readme has:
Calling Julia from R
See the JuliaCall library to call Julia from R.
But the link to it is failing, I believe it moved, others took over, and needs to point to that… i.e. to GitHub - JuliaInterop/JuliaCall: Embed Julia in R and/or to CRAN: Package JuliaCall since the former is only almost up-to-date, NEWS at both places have the important 1.17.6 patch release (fixing breakage in Julia), but it’s not under releases at former, for some reason only 1.17.5 so what do you get if installing?