Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
egoecho opened this issue May 27, 2019 · 3 comments
Closed

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

egoecho opened this issue May 27, 2019 · 3 comments

Comments

@egoecho
Copy link

egoecho commented May 27, 2019

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?

@simonbyrne
Copy link
Member

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

@egoecho
Copy link
Author

egoecho commented May 29, 2019

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]

@palday
Copy link
Collaborator

palday commented Jun 22, 2020

@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. 😄

@palday palday pinned this issue Jun 9, 2023
@palday palday closed this as completed Jun 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants