Why rcopy(R"array(1)") return 1 rather than [1]?
One feature in the RCall confused me.
julia> a=R"array(1)"
RObject{RealSxp}
[1] 1
julia> b=R"1"
RObject{RealSxp}
[1] 1
Although both print the same "1", R considered them as different data types:
julia> rcall(R"is.array", a)
RObject{LglSxp}
[1] TRUE
julia> rcall(R"is.array", b)
RObject{LglSxp}
[1] FALSE
However, rcopy(...) returns the same value 1:
julia> rcopy(a)
1.0
julia> rcopy(b)
1.0
I think rcopy(a)=[ 1.0 ] could be more reasonable. Is there any reason why regard array(1) as 1?
The problem is that R doesn't have scalars: "scalars" are just 1-element vectors. I made the judgement call here to have 1-element vectors copied as scalars, since that is presumably what most people would find useful.
Note that in R an "array" is a multidimensional structure, i.e. a vector is not an array by default:
> is.array(c(1,2,3))
[1] FALSE
If you want rcopy to return a specific type out, you can specify it as the first argument, e.g. rcopy(Vector, a).
Thanks for your explanation. I think rcopy(Vector, ...) is a useful design since the output type is always expected. Maybe this syntax can be officially documented.
Alternatively, I may propose to add a flag in rcopy, like rcopy(R"1", drop=true) = 1.0 (default) rcopy(R"1", drop=false) = Vector[1.0] rcopy(R"matrix(1)", drop=false) = Matrix[1.0] rcopy(R"array(1)", drop=false) = Array{Float64, N}[1.0]
@simonbyrne This seems like a good issue to pin and close so that users see it right away when they come to ask what's going wrong with 1-element vectors. :smile: