JuliaInterop / JuliaCall

Embed Julia in R
https://non-contradiction.github.io/JuliaCall/index.html
Other
267 stars 36 forks source link

[WIP] Automatic wrapping of julia packages #118

Closed Non-Contradiction closed 5 years ago

Non-Contradiction commented 5 years ago

Implement the automatic wrapping functionality for julia functions and packages. Hope it is helpful for package developers using JuliaCall.

Description

Related Issue

Related to https://github.com/JuliaDiffEq/diffeqr/issues/16

Example

The new code implements JuliaCall:::julia_function and JuliaCall:::julia_pkg_import, both unexported currently, which wrap functions and packages automatically. The usage is as follows:

JuliaCall::julia_library("DifferentialEquations")
JuliaCall::autowrap("DiffEqBase.AbstractODESolution", fields = c("t","u"))
f <- function(u,p,t) -u
u0 = 0.5
tspan = JuliaCall::julia_eval("(0., 1.)")
de <- JuliaCall:::julia_pkg_import("DifferentialEquations", c("ODEProblem", "solve"))
prob = de$ODEProblem(f, u0, tspan)
sol = de$solve(prob)

In progress.

Non-Contradiction commented 5 years ago

Push more commits into the branch with a more refined API. Now the example should look like this:

## The above should be safe to go into the function level code.

de <- JuliaCall:::julia_pkg_import("DifferentialEquations",
                                   c("ODEProblem", "solve"))
JuliaCall:::julia_pkg_hook(de,
                           function(){
                               JuliaCall::autowrap("DiffEqBase.AbstractODESolution",
                                                   fields = c("t","u"))
                           })
time_range <- function(a, b) JuliaCall::julia_call("tuple",
                                                   a, b)

## the user code follows

f <- function(u,p,t) -u
u0 = 0.5
tspan = time_range(0, 1)
prob = de$ODEProblem(f, u0, tspan)
sol = de$solve(prob)
ChrisRackauckas commented 5 years ago

Out of curiousity, does it the call on sol work for the continuous output, i.e. sol(0.4) there?

Non-Contradiction commented 5 years ago

R does not support the callable object syntax. sol is not recognized as a function in R so R will not think sol() is valid. I am not sure whether I can modify the internal of JuliaCall to make sol a function so the syntax is possible. Once the syntax works, the functionality should follows. Or is there any suggestion for a new syntax to mimic callable object syntax in R?

ChrisRackauckas commented 5 years ago

Maybe it can be something like sol$.(0.4)? I'm fine with any reasonable syntax on this: that's fixable just by documenting it.

Non-Contradiction commented 5 years ago

With the new commits, sol$.(0.4) will give the result as sol(0.4) in julia. In the commits, $juliaobject$.()` is the syntax to mimic callable objects in Julia, and one cavity is that it doesn't support keyword arguments currently.

Non-Contradiction commented 5 years ago

I will first merge this PR and get a new release with all the new functionalities and bug fixes. More work will be done in future PRs.