# Julia with R using JuliaCall

You'll need to install the JuliaCall package in R for this post.

Start by setting up a julia session with julia_setup() - and note that this can take time to complete. This creates an environment, which you don’t need to know anything about to use JuliaCall, but is covered in the Advanced R book, here, for further interest. The julia_library function allows you to load the julia packages that you will need for your work (in the same way as you would use library in R). As an example, here we have the Distributions package so we’ll use it to generate random numbers further down. You can use julia_install_package, julia_install_package_if_needed and julia_update_package in the obvious way.

library(JuliaCall)
julia <- julia_setup() # Note that you made need to use the JULIA_HOME option depending on where your installed binary is kept

## Julia version 0.6.2 at location /Applications/Julia-0.6.app/Contents/Resources/julia/bin will be used.## Loading setup script for JuliaCall...## Finish loading setup script for JuliaCall.
julia_install_package_if_needed("Distributions") # if you don't already have the package installed
julia_library("Distributions")

The julia_command function allows you to evaluate string commands in julia. Many blogs that use JuliaCall given an example using only this with a few lines of basic operations. But if you want to use julia within R for any real purpose, your julia code is going to be much longer than this, with many functions and possibly files. For that scenario, you’ll want to use the julia_source function on all your julia scripts, and then use the julia_command function.

julia_command("1 + 2")

## 3
writeLines("
function num_gen(n::Float64)
  rand(Normal(), convert(Int64, n))
end", 
"num_gen_fn.jl")

julia_source("num_gen_fn.jl")
julia_command("num_gen(3)")

You can send variables to julia using the julia_assign function. In the following, we assign n as 2 and send it to julia. If we then reassign n in R, this has not been reassigned in julia. Also note that passing variables between R and julia can be a pain if you are wanting to use integers (as in the function above) because values passed from R will never be initially converted to an Int64 in julia.

n <- 2
julia_assign("n", n)

julia_command("num_gen(n)")

## 2-element Array{Float64,1}:## -0.81389 ## -0.814427
n <- 5
julia_command("num_gen(n)") # n has not been reassigned to 5 in julia because we didn't assign it

## 2-element Array{Float64,1}:## -0.446079## 1.97775

The behaviour of julia_command is to evaluate a string but without returning the result. So if we want to get those random numbers back from julia, we just get NULL. To evaluate a string command and return the result for use in R, there is julia_eval.

y <- julia_eval("num_gen(n)")
y

## [1] -0.4522313 -1.9705432

An alternative way to call functions set in julia is using julia_call. This basically avoids having to assign and then command. However, if you need to manipulate the variable in any way in julia before the function is applied, then command/eval would still be more useful. Additionally, there is a pipeline operator that can be used for julia.

julia_call("num_gen", 4)

## [1] 0.02119496 -2.28169271 -1.21789192 0.38863493
5 %>J% num_gen()

## [1] -0.5802326 -1.9514063 0.7930599 0.2248462 -0.1320907