[Table of Contents]
Package JuliaCall
is an R interface to Julia
, which is a high-level,
high-performance dynamic programming language for numerical computing,
see https://julialang.org/ for more information. Below is an image for
Mandelbrot set.
JuliaCall brings more than 100 times speedup of the calculation!
See
https://github.com/Non-Contradiction/JuliaCall/tree/master/example/mandelbrot
for more
information.
To use package JuliaCall
, you first have to install
Julia
on your computer, you can download a
generic binary from https://julialang.org/downloads/ and add it to
your path, and then you can install JuliaCall
just like any other R
packages by
install.packages("JuliaCall")
Note that currently Julia v0.6.x
, Julia v0.7.0
and Julia v1.0
are
all supported by JuliaCall
. But note that some Julia
packages may
not work on Julia v1.0
and you may need to use Julia v0.7.0
for
transition purposes.
You can get the development version of JuliaCall
by
devtools::install_github("Non-Contradiction/JuliaCall")
library(JuliaCall)
## Do initial setup
julia <- julia_setup()
#> Julia version 1.0.0 at location /Applications/Julia-1.0.app/Contents/Resources/julia/bin will be used.
#> Loading setup script for JuliaCall...
#> Finish loading setup script for JuliaCall.
## If you want to use a julia at a specific location, you could do the following:
## julia_setup(JULIA_HOME = "the folder that contains julia binary"),
## or you can set JULIA_HOME in command line environment or use `options(...)`
## Different ways for using Julia to calculate sqrt(2)
# julia$command("a = sqrt(2);"); julia$eval("a")
julia_command("a = sqrt(2);"); julia_eval("a")
#> [1] 1.414214
julia_eval("sqrt(2)")
#> [1] 1.414214
julia_call("sqrt", 2)
#> [1] 1.414214
julia_eval("sqrt")(2)
#> [1] 1.414214
julia_assign("x", sqrt(2)); julia_eval("x")
#> [1] 1.414214
julia_assign("rsqrt", sqrt); julia_call("rsqrt", 2)
#> [1] 1.414214
2 %>J% sqrt
#> [1] 1.414214
## You can use `julia$exists` as `exists` in R to test
## whether a function or name exists in Julia or not
julia_exists("sqrt")
#> [1] TRUE
julia_exists("c")
#> [1] FALSE
## Functions related to installing and using Julia packages
julia_install_package_if_needed("Optim")
julia_installed_package("Optim")
#> [1] "0.16.0"
julia_library("Optim")
Make sure the Julia
installation is correct. JuliaCall
is able to
find Julia
on PATH, and there are three ways for JuliaCall
to find
Julia
not on PATH.
- Use
julia_setup(JULIA_HOME = "the folder that contains julia binary")
- Use
options(JULIA_HOME = "the folder that contains julia binary")
- Set
JULIA_HOME
in command line environment.
- One way to get help for julia functions is just using
julia$help
like the following example:
julia_help("sqrt")
#> ```
#> sqrt(x)
#> ```
#>
#> Return $\sqrt{x}$. Throws [`DomainError`](@ref) for negative [`Real`](@ref) arguments. Use complex negative arguments instead. The prefix operator `√` is equivalent to `sqrt`.
#>
#> # Examples
#>
#> ```jldoctest; filter = r"Stacktrace:(\n \[[0-9]+\].*)*"
#> julia> sqrt(big(81))
#> 9.0
#>
#> julia> sqrt(big(-81))
#> ERROR: DomainError with -8.1e+01:
#> NaN result for non-NaN input.
#> Stacktrace:
#> [1] sqrt(::BigFloat) at ./mpfr.jl:501
#> [...]
#>
#> julia> sqrt(big(complex(-81)))
#> 0.0 + 9.0im
#> ```
#>
#> ```
#> sqrt(A::AbstractMatrix)
#> ```
#>
#> If `A` has no negative real eigenvalues, compute the principal matrix square root of `A`, that is the unique matrix $X$ with eigenvalues having positive real part such that $X^2 = A$. Otherwise, a nonprincipal square root is returned.
#>
#> If `A` is symmetric or Hermitian, its eigendecomposition ([`eigen`](@ref)) is used to compute the square root. Otherwise, the square root is determined by means of the Björck-Hammarling method [^BH83], which computes the complex Schur form ([`schur`](@ref)) and then the complex square root of the triangular factor.
#>
#> [^BH83]: Åke Björck and Sven Hammarling, "A Schur method for the square root of a matrix", Linear Algebra and its Applications, 52-53, 1983, 127-140. [doi:10.1016/0024-3795(83)80010-X](https://doi.org/10.1016/0024-3795(83)80010-X)
#>
#> # Examples
#>
#> ```jldoctest
#> julia> A = [4 0; 0 4]
#> 2×2 Array{Int64,2}:
#> 4 0
#> 0 4
#>
#> julia> sqrt(A)
#> 2×2 Array{Float64,2}:
#> 2.0 0.0
#> 0.0 2.0
#> ```
-
The GitHub Pages for this repository host the documentation for the development version of
JuliaCall
: https://non-contradiction.github.io/JuliaCall/. -
And you are more than welcome to contact me about
JuliaCall
at lch34677@gmail.com or cxl508@psu.edu.
If you are interested in developing an R package which is an interface
for a Julia package, JuliaCall
is an ideal choice for that!
Basically you only need to find the Julia function or Julia module you
want to have in R
and then just using
the module and call
the
function. Several examples are:
diffeqr
is a package for solving differential equations inR
. It utilizes DifferentialEquations.jl for its core routines to give high performance solving of ordinary differential equations (ODEs), stochastic differential equations (SDEs), delay differential equations (DDEs), and differential-algebraic equations (DAEs) directly inR
.convexjlr
is anR
package for Disciplined Convex Programming (DCP) by providing a high level wrapper forJulia
packageConvex.jl
.convexjlr
can solve linear programs, second order cone programs, semidefinite programs, exponential cone programs, mixed-integer linear programs, and some other DCP-compliant convex programs throughConvex.jl
.ipoptjlr
provides anR
interface to theIpopt
nonlinear optimization solver. It provides a simple high-level wrapper forJulia
package [Ipopt.jl
] (https://github.com/JuliaOpt/Ipopt.jl).- Julia MixedModels from R
illustrates how to use
JuliaCall
andJulia
packageMixedModels.jl
to build mixed models inR
. autodiffr
provides automatic differentiation to nativeR
functions by wrappingJulia
packagesForwardDiff.jl
andReverseDiff.jl
throughJuliaCall
, which is a work in progress.
If you have any issues in developing an R
package using JuliaCall
,
you may report it using the link:
https://github.com/Non-Contradiction/JuliaCall/issues/new. Or email me
at lch34677@gmail.com or cxl508@psu.edu.
JuliaCall
is under active development now. Any suggestion or issue
reporting is welcome! You may report it using the link:
https://github.com/Non-Contradiction/JuliaCall/issues/new. Or email me
at lch34677@gmail.com or cxl508@psu.edu.
And if you encounter some issues which crash R
or RStudio
, then you
may have met segfault errors. I am very sorry. And I am trying my best
to remove errors like that. It will be much appreciated if you can
- download the source of
JuliaCall
from Github, - open
JuliaCall.Rproj
in your RStudio or openR
from the directory where you download the source ofJuliaCall
, - run
devtools::check()
to see the result ofR CMD check
forJuliaCall
on your machine, - and paste the result to the issue report.
RCall.jl
is aJulia
package which embedsR
inJulia
.JuliaCall
is inspired byRCall.jl
and depends onRCall.jl
for many functionalities like much of the type conversion betweenR
andJulia
.XRJulia
is anR
package based on John Chambers’XR
package and allow for structured integration ofR
withJulia
. It connects toJulia
and uses JSON to transfer data betweenJulia
andR
.RJulia
is anR
package which embedsJulia
inR
as well asJuliaCall
. It is not on CRAN yet and I haven’t tested it.