JuliaInterop / JuliaCall

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

Callables do not support keyword arguments #165

Closed devmotion closed 3 years ago

devmotion commented 3 years ago

Callables do not support keyword arguments currently:

> library(JuliaCall)
> julia_setup()
> julia_command("struct Callable end")
> julia_command("(::Callable)(x...; a=3.0, b=2) = print(\"a=\", a, \", b=\", b)")
> r <- julia_eval("Callable()")
> r$.(1, 2, 3, a=3.2)
Error: Error happens in Julia.
MethodError: no method matching apply(::Callable, ::Float64, ::Float64, ::Float64; a=3.2)
Closest candidates are:
  apply(::Any, ::Any...) at /home/david/R/x86_64-pc-linux-gnu-library/4.0/JuliaCall/julia/setup.jl:233 got unsupported keyword argument "a"
Stacktrace:
 [1] docall(call1::Ptr{Nothing})
   @ Main.JuliaCall ~/R/x86_64-pc-linux-gnu-library/4.0/JuliaCall/julia/setup.jl:168
> sessionInfo()
R version 4.0.5 (2021-03-31)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Arch Linux

Matrix products: default
BLAS:   /usr/lib/libopenblasp-r0.3.14.so
LAPACK: /usr/lib/liblapack.so.3.9.1

locale:
 [1] LC_CTYPE=en_US.UTF-8          LC_NUMERIC=C                 
 [3] LC_TIME=en_US.UTF-8           LC_COLLATE=en_US.UTF-8       
 [5] LC_MONETARY=en_US.UTF-8       LC_MESSAGES=en_US.UTF-8      
 [7] LC_PAPER=en_US.UTF-8          LC_NAME=en_US.UTF-8          
 [9] LC_ADDRESS=en_US.UTF-8        LC_TELEPHONE=en_US.UTF-8     
[11] LC_MEASUREMENT=en_US.UTF-8    LC_IDENTIFICATION=en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] JuliaCall_0.17.2

loaded via a namespace (and not attached):
[1] compiler_4.0.5 tools_4.0.5    Rcpp_1.0.6     knitr_1.31     xfun_0.22
devmotion commented 3 years ago

I'd like to help to fix this issue if it is feasible since I'd like to change the API of my package without dropping support for R, but unfortunately I don't know where to look and where to start in JuliaCall.

Non-Contradiction commented 3 years ago

Thank you very much for the feedback and the use of JuliaCall for your package!

I just made a commit that should fix the problem.
The fix is quite straightforward. The message in the traceback

apply(::Any, ::Any...) at /home/david/R/x86_64-pc-linux-gnu-library/4.0/JuliaCall/julia/setup.jl:233 got unsupported keyword argument "a"

points out to the lines in setup.jl that the original apply function does not support keyword arguments. https://github.com/Non-Contradiction/JuliaCall/blob/6431e0eca7e0c3f8b073d8dce2fcaa1beceddd88/inst/julia/setup.jl#L241-L243 So I just made a little modification to the function to accept keyword arguments.

devmotion commented 3 years ago

Great, thanks a lot!