Skip to content

Commit

Permalink
support NamedArrays
Browse files Browse the repository at this point in the history
  • Loading branch information
randy3k committed Mar 22, 2017
1 parent f15c093 commit f1c55d4
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 6 deletions.
1 change: 1 addition & 0 deletions REQUIRE
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ DataFrames 0.9
NullableArrays 0.1.0
CategoricalArrays 0.1.0
AxisArrays 0.0.6
NamedArrays 0.5.3
Compat 0.20.0
@windows WinReg 0.2.0
19 changes: 17 additions & 2 deletions docs/src/conversions.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Conversions

RCall supports conversions to and from most base Julia types and popular Statistics packages, e.g., `DataFrames`, `DataArrays`, `NullableArrays`, `CategoricalArrays` and `AxisArrays`.
RCall supports conversions to and from most base Julia types and popular Statistics packages, e.g., `DataFrames`, `DataArrays`, `NullableArrays`, `CategoricalArrays` `NamedArrays` and `AxisArrays`.

```@setup 1
using RCall
using DataFrames
using NamedArrays
using AxisArrays
```

Expand Down Expand Up @@ -94,11 +95,25 @@ To avoid the sanitization, use `sanitize` option.
rcopy(R"data.frame(a.b = 1:10)"; sanitize = false)
```

## NamedArrays

```@example 1
# Julia -> R
aa = NamedArray([1,2,3], [["a", "b", "c"]], [:id])
r = RObject(aa)
```

```@example 1
# R -> Julia
rcopy(r)
```


## AxisArrays

```@example 1
# Julia -> R
aa = AxisArray([1,2,3], Axis{:name}(["a", "b", "c"]))
aa = AxisArray([1,2,3], Axis{:id}(["a", "b", "c"]))
r = RObject(aa)
```

Expand Down
3 changes: 2 additions & 1 deletion src/RCall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ using Compat
using DataFrames
# using DataTables
using NullableArrays, CategoricalArrays
using AxisArrays
using AxisArrays, NamedArrays

import DataStructures: OrderedDict

Expand All @@ -31,6 +31,7 @@ include("convert/dataframe.jl")
include("convert/datatable.jl")
include("convert/datetime.jl")
include("convert/axisarray.jl")
include("convert/namedarray.jl")
include("convert/default.jl")
include("eventloop.jl")
include("eval.jl")
Expand Down
2 changes: 0 additions & 2 deletions src/convert/axisarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ function rcopy{S<:VectorSxp}(::Type{AxisArray}, r::Ptr{S})
AxisArray(rcopy(Array, r), [Axis{dsym[i]}(rcopy(n)) for (i,n) in enumerate(dnames)]...)
end


for S in (:IntSxp, :RealSxp, :CplxSxp, :LglSxp, :StrSxp)
@eval begin
function sexp(::Type{$S}, aa::AxisArray)
rv = protect(sexp($S, aa.data))
try
d = OrderedDict(
k => v.val for (k, v) in zip(axisnames(aa), axes(aa)))
setattrib!(rv, Const.ClassSymbol, "array")
setattrib!(rv, Const.DimSymbol, collect(size(aa)))
setattrib!(rv, Const.DimNamesSymbol, d)
finally
Expand Down
3 changes: 2 additions & 1 deletion src/convert/default.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,14 @@ for typ in [:NullableCategoricalArray, :CategoricalArray]
@eval sexp(v::$typ) = sexp(IntSxp, v)
end

# AxisArray
# AxisArray and NamedArray
for (J,S) in ((:Integer,:IntSxp),
(:AbstractFloat, :RealSxp),
(:Complex, :CplxSxp),
(:Bool, :LglSxp),
(:AbstractString, :StrSxp))
@eval sexp{T<:$J}(aa::AxisArray{T}) = sexp($S, aa)
@eval sexp{T<:$J}(aa::NamedArray{T}) = sexp($S, aa)
end

# DataTime
Expand Down
23 changes: 23 additions & 0 deletions src/convert/namedarray.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function rcopy{S<:VectorSxp}(::Type{NamedArray}, r::Ptr{S})
dnames = getattrib(r, Const.DimNamesSymbol)
isnull(dnames) && error("r has no dimnames")
d = [rcopy(Vector{String}, n) for n in dnames]
NamedArray(rcopy(Array, r), d, rcopy(Vector{Symbol}, getnames(dnames)))
end

for S in (:IntSxp, :RealSxp, :CplxSxp, :LglSxp, :StrSxp)
@eval begin
function sexp(::Type{$S}, na::NamedArray)
rv = protect(sexp($S, na.array))
try
d = OrderedDict(
k => v for (k, v) in zip(dimnames(na), names(na)))
setattrib!(rv, Const.DimSymbol, collect(size(na)))
setattrib!(rv, Const.DimNamesSymbol, d)
finally
unprotect(1)
end
rv
end
end
end
8 changes: 8 additions & 0 deletions test/convert/namedarray.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using NamedArrays

# NamedArray
aa = rcopy(NamedArray, R"Titanic")
@test size(aa) == (4, 2, 2, 2)
@test length(names(aa)[1]) == 4
@test_throws ErrorException rcopy(NamedArray, R"c(1,1)")
@test names(getattrib(RObject(aa), :dimnames))[1] == :Class
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ tests = ["basic",
"convert/datatable",
"convert/datetime",
"convert/axisarray",
"convert/namedarray",
"library",
"render",
"repl",
Expand Down

0 comments on commit f1c55d4

Please sign in to comment.