JuliaInterop / JuliaCall

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

JuliaCall Converts Int to Float #167

Closed ParadaCarleton closed 3 years ago

ParadaCarleton commented 3 years ago

Code:

> julia_call("gpd.gpdfit", x, TRUE, 30, TRUE, 
+               need_return = "R")

Returns this:

Error: Error happens in Julia.
MethodError: no method matching gpdfit(::Vector{Float64}, ::Bool, ::Float64, ::Bool)
Closest candidates are:
  gpdfit(::AbstractVector{T} where T, ::Bool, !Matched::Int64, ::Bool) at /home/lime/Documents/GitHub/JuLOOa/src/gpd.jl:37
  gpdfit(::AbstractVector{T} where T, ::Bool) at /home/lime/Documents/GitHub/JuLOOa/src/gpd.jl:37
  gpdfit(::AbstractVector{T} where T, ::Bool, !Matched::Int64) at /home/lime/Documents/GitHub/JuLOOa/src/gpd.jl:37

It looks like it's possible to get RCall to realize it's an integer using Integer(30)[1], but this is quite ugly.

Non-Contradiction commented 3 years ago

Thanks for the feedback!

In R, 30 is automatically treated as a float instead of an int. And the integer in R needs to come with the suffix "L". This is often unnoticeable because R typically doesn't distinguish between float and int, but the difference exists:

> typeof(1)
[1] "double"
> typeof(1L)
[1] "integer"
> 

So for things to work, we need to use

> julia_call("gpd.gpdfit", x, TRUE, 30L, TRUE, 
+               need_return = "R")

Note the 30L instead of 30, because we need to set this as a "correct" R integer and JuliaCall will do the corresponding conversion. By the way, R does many things implicitly/automatically to treat float as int. For example,

> (1:3)[1]
[1] 1
> (1:3)[1.1]
[1] 1
> (1:3)[1.6]
[1] 1
ParadaCarleton commented 3 years ago

Thanks!