user3507085
user3507085

Reputation: 720

apply function from R to julia

I am a total newbie in Julia world and I am a trying to call the julia mapslices function from R. However I have this following issue:

library(XRJulia)
japply=JuliaFunction(juliaEval("function(a) return(mapslices(sum,a,[1])) end"))
 a=array(runif(16),c(4,4))
juliaGet(japply(juliaSend(a)))
#     [,1]     [,2]     [,3]    [,4]
#[1,] 1.083545 2.426658 2.310691 1.44339
#But
a=array(runif(32),c(4,4,2))
juliaGet(japply(juliaSend(a)))
#  Error in checkSlotAssignment(object, name, value) : 
# ‘.Data’ is not a slot in class “array”

What am I doing wrong? Thank you

Upvotes: 1

Views: 490

Answers (1)

Consistency
Consistency

Reputation: 2922

You could also try my package JuliaCall, which embeds Julia in R. The usage is quite similar to XRJulia in this case. The multi-dimensional array in Julia just converts to multi-dimensional array in R automatically.

library(JuliaCall)
julia_setup()
japply=julia_eval("function(a) return(mapslices(sum,a,[1])) end")
a=array(runif(16),c(4,4))
japply(a)
#     [,1]     [,2]     [,3]    [,4]
#[1,] 1.083545 2.426658 2.310691 1.44339
a=array(runif(32),c(4,4,2))
japply(a)
#, , 1
#
#         [,1]     [,2]     [,3]    [,4]
#[1,] 3.119738 3.116167 2.299303 1.96874
#
#, , 2
#
#         [,1]     [,2]      [,3]     [,4]
#[1,] 1.578722 1.280093 0.6427822 2.786489

The main difference between XRJulia and JuliaCall is that XRJulia connects to Julia in R while JuliaCall embeds Julia in R. JuliaCall has performance advantage over XRJulia when you need to transfer large vectors or matrices between R and Julia, but it will do more work in the startup of Julia (especially for the first time).

Upvotes: 1

Related Questions