SOCR ≫ DSPA ≫ DSPA2 Topics ≫

This DSPA Appendix shows examples of dynamic systems, chaotic, spatiotemporal changes, and biocomplexity modeling using R and [Julia(https://julialang.org/) integration.

In this appendix, we use the R package JuliaCall as a Julia language wrapper, and various Julia packages, such as JuliaDynamics and DynamicalSystems.

Julia is a high-performance dynamic programming language for numerical computing that can be used for efficient solution to optimization problems and other demanding scientific computations in R. JuliaCall provides direct integration between R and Julia, where Julia functions can be called just like any other R function. Similarly, R functions can be called in the Julia environment (chink blocks in the Rmd script). Both invocations provide automatic type conversion. JuliaConnectoR is another R package that provides a functionally oriented interface for integrating Julia and R functionality. Imported Julia functions can be called as R functions and data structures are converted automatically. Also, the package XRJulia provides an interface from R to computations in Julia, based on the interface structure described in the book “Extending R” by John M. Chambers.

1 Basic Julia code chunks embedded in Rmd

First load the necessary R-Julia connector libraries.

# R-block of code
# The first time - install these libs
#
# install.packages("JuliaCall")
# install.packages("JuliaConnectoR")
# library(JuliaCall)
# install_julia()  # This is needed just once to install Julia on the system!!!
# install.packages("XRJulia")  # Install the package if not already installed

# Set the JULIA_NUM_THREADS environment variable in R:
library(XRJulia)
library(JuliaCall)

Sys.setenv(JULIA_NUM_THREADS = 10)
julia <- julia_setup()  # This will start Julia with 10 threads

# findJulia()  # This will locate the Julia installation
# julia <- JuliaSession()  # Start Julia and assign the session to the `julia` object

# Check that the julia object is properly initialized by running a simple Julia command:
julia_command("1 + 1")  #  # Should return 2
## 2
# Verify the number of threads in Julia:
julia_command("using Base.Threads; Threads.nthreads() = 10")  # Manually set threads
# Should return 10
message(paste0("The Number of Cores Julia starts with is: ", julia_command("Threads.nthreads()")$out))  
# julia_command("using Base.Threads; Threads.nthreads()")  # Should return 10
# julia_command("ENV[\"JULIA_NUM_THREADS\"]")  # Should return "10"
# JuliaConnectoR::stopJulia()     # Terminate the Julia session 

Next, demonstrate Julia code chunk for a simple calculation, \(a=\sqrt{10}\). Note that this just computes the result, which can then be printed either in Julia or in R code chunk.

## 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(10);
print(a);

Next is a subsequent R code block reporting the result from the Julia chunk calculation.

# report the result from the Julia chunk calculation
message(paste0("testing sqrt(10) in Julia: ", julia_eval("a"))