Skip to contents

Helpers for converting model specifications in R to Julia equivalents

Usage

is_jl(x, type)

jl_put(x)

jl_get(x)

jl(expr, ..., .R = FALSE, .passthrough = FALSE)

jl_dict(...)

Arguments

x

An object

type

Type of Julia object to additional test for

expr

A string of Julia code

...

Interpolated elements. In the case of jl():.

  • If all named, elements are introduced as Julia variables in the expr

  • If all unnamed, elements are interpolated into expr via sprintf()

.R

Whether to simplify and return as R object, if possible.

.passthrough

Whether to return expr as-is if it's already a Julia object. Mostly for internal use.

Value

A Julia object of type <JuliaProxy>

Examples

# \donttest{
# (general) Use `jl()` to evaluate arbitrary Julia expressions from string
jl("1 .+ [1,3]")
#> Starting Julia ...
#> <Julia object of type Vector{Int64}>
#> 2-element Vector{Int64}:
#>  2
#>  4

# `jl()` takes elements in `...` that you can reference in the expression
jl("1 .+ a", a = c(1L, 3L)) # Named arguments are introduced as variables
#> <Julia object of type Vector{Int64}>
#> 2-element Vector{Int64}:
#>  2
#>  4
jl("1 .+ %s", "[1,2]") # Unnamed arguments are interpolated via `sprintf()`
#> <Julia object of type Vector{Int64}>
#> 2-element Vector{Int64}:
#>  2
#>  3

# Use `is_jl()` to test if object is a Julia (`<JuliaProxy>`) object
is_jl(jl("1"))
#> [1] TRUE

# Use `jl_put()` and `jl_get()` to transfer data between R and Julia
jl_put(1L)
#> <Julia object of type Int64>
#> 1
identical(jl_get(jl_put(1L)), 1L)
#> [1] TRUE

# `jl_dict()` opinionatedly constructs a Dictionary data type
## Basic `list()`-like usage
jl_dict(age = 20:25, sex = c("M", "F"))
#> <Julia object of type Dict{Symbol, Any}>
#> Dict{Symbol, Any} with 2 entries:
#>   :age => [20, 21, 22, 23, 24, 25]
#>   :sex => ["M", "F"]
## Splats when a single list is supplied
jl_dict(list(a = 1, b = 2))
#> <Julia object of type Dict{Symbol, Any}>
#> Dict{Symbol, Any} with 2 entries:
#>   :a => [1.0]
#>   :b => [2.0]
## Wrap scalars in `I()` to prevent vector conversion
jl_dict(a = 1:2, b = 3:4, c = I(5))
#> <Julia object of type Dict{Symbol, Any}>
#> Dict{Symbol, Any} with 3 entries:
#>   :a => [1, 2]
#>   :b => [3, 4]
#>   :c => 5.0

stop_julia()
# }