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}" or "{r, engine=‘julia’}”.
Below is how it looks like when using JuliaCall as language engine for Julia.
## 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)
a = sqrt(2);
## And lots of different types of display are supported in JuliaCall.
## Like markdown and plots.
@doc sqrt
sqrt(x)
Return ${x}$. Throws DomainError for negative Real arguments. Use complex negative arguments instead. The prefix operator √ is equivalent to sqrt.
using Gadfly
Gadfly.plot(y = [1 2 3])
using Plots
pyplot()
Plots.plot(sin,(x->begin sin(2x) end),0,2pi,line=4,leg=false,fill=(0,:orange))
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_string("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)
JuliaCall::julia_library("Plots")
JuliaCall::julia_command("pyplot()")
JuliaCall::julia_command("Plots.plot(sin,(x->begin sin(2x) end),0,2pi,line=4,leg=false,fill=(0,:orange))")
Currently only R Markdown html output is fully supported by JuliaCall.
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.