How to run a jl file (julia) in R

348 views Asked by At

I know that the JuliaCall package allows to us use the Julia language in R. But I have a jl script with complex instructions and I would like to run in the R language! For example, suposse that my jl file is "myscript.jl" return a certain array A.

#install.packages("JuliaCall")
library(JuliaCall)

Is there some function to run the jl file?

A <- somefunction("myscript.jl")

some help?

1

There are 1 answers

2
Antonello On BEST ANSWER

It is indeed quite simple if you have Julia on path.

First create a file juliaScripts.jl with content:

function getAnElement(array,n)
    return array[n]
end

Then in R you just do:

> install.packages("JuliaCall")
> library(JuliaCall)
> julia_setup() # on every new R session !
> julia_source("juliaScript.jl")
> out <- julia_call("getAnElement",c(10,20,30),2)
> out
[1] 20

Note that the R vector has been automatically converted to a Julia Array.

Not to make advertising, but more details on interfacing R <-> Julia are on my Apress(2019) book "Julia Quick Syntax reference" on Chapter 7 "Interfacing Julia with other languages" (I shouldn't say it, but you can easily find the pdf online in well-known sites...)