Use JuliaCall as Julia Engine in R Markdown

To use JuliaCall package for julia engine in R Markdown document. Just set the engine for julia to JuliaCall::eng_juliacall like this:

knitr::opts_chunk$set(echo = TRUE)
knitr::knit_engines$set(julia = JuliaCall::eng_juliacall)

And in the julia code chunk, just use “```{julia}”.

Below is how it looks like when using JuliaCall as language engine for Julia.

How Does JuliaCall in R Markdown Look Like?

## This is a julia language chunk.
## In julia, the command without ending semicolon will trigger the display
## so is JuliaCall package. 
## The julia display will follow immediately after the corresponding command
## just as the R code in R Markdown.
a = sqrt(2)
1.4142135623730951
a = sqrt(2);

## And lots of different types of display are supported in JuliaCall.
## Like markdown and plots.
@doc sqrt
sqrt(x)

Return $\sqrt{x}$. Throws DomainError for negative Real arguments. Use complex negative arguments instead. The prefix operator is equivalent to sqrt.

Support for Plots.jl

Plots.jl is an easy to use and powerful julia package for plotting, https://github.com/JuliaPlots/Plots.jl. And JuliaCall supports Plots.jl of cource!!

using Plots

PyPlot backend

pyplot()
Plots.PyPlotBackend()
Plots.plot(sin,(x->begin sin(2x) end),0,2pi,line=4,leg=false,fill=(0,:orange))

GR backend

gr()
Plots.GRBackend()
Plots.plot(sin,(x->begin sin(2x) end),0,2pi,line=4,leg=false,fill=(0,:orange))
-0.75 -0.50 -0.25 0.00 0.25 0.50 0.75 -0.75 -0.50 -0.25 0.00 0.25 0.50 0.75

PlotlyJS backend

plotlyjs()
Plots.PlotlyJSBackend()
Plots.plot(sin,(x->begin sin(2x) end),0,2pi,line=4,leg=false,fill=(0,:orange))

Plotly backend

plotly()
Plots.PlotlyBackend()
Plots.plot(sin,(x->begin sin(2x) end),0,2pi,line=4,leg=false,fill=(0,:orange))

Get Access to Julia in R Chunk

And you can also get access to julia variables in R code chunk quite easily using JuliaCall, for example:

## This is a R language chunk.
## In the previous julia chunk, we define variable a, 
## we can use functions in JuliaCall to get access to it.
JuliaCall::julia_eval("a")
## [1] 1.414214
## And you can also get access to Julia libraries and functions.
JuliaCall::julia_library("Gadfly")
JuliaCall::julia_call("Gadfly.plot", y = c(1:3), 
                      need_return = FALSE, show_value = TRUE)
-1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5.0 -2 0 2 4 6 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 3.2 3.4 3.6 3.8 4.0 4.2 4.4 4.6 4.8 5.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5.0 -2 0 2 4 6 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 3.2 3.4 3.6 3.8 4.0 4.2 4.4 4.6 4.8 5.0 y

Current Limitations

Currently only R Markdown html output is fully supported by JuliaCall.

Custom Style of Julia Display in R Markdown Using CSS

When using JuliaCall in R Markdown, every julia display will be wrapped in a div with class = "JuliaDisplay", so you could customize the looking of julia display using css, for example, I use

    <style>
    .JuliaDisplay {
        color: blue;
    }
    </style>

in this document.