RCall.jl icon indicating copy to clipboard operation
RCall.jl copied to clipboard

Why rcopy(R"array(1)") return 1 rather than [1]?

Open egoecho opened this issue 6 years ago • 3 comments

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?

egoecho avatar May 27 '19 11:05 egoecho

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).

simonbyrne avatar May 28 '19 18:05 simonbyrne

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]

egoecho avatar May 29 '19 08:05 egoecho

@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:

palday avatar Jun 22 '20 10:06 palday