Supported Conversions

Supported Conversions

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

Base Julia Types

# Julia -> R
a = robject(1)
RObject{IntSxp}
[1] 1
# R -> Julia
rcopy(a)
1
# Julia -> R
a = robject([1.0, 2.0])
RObject{RealSxp}
[1] 1 2
# R -> Julia
rcopy(a)
2-element Array{Float64,1}:
 1.0
 2.0

Dictionaries

# Julia -> R
d = Dict(:a => 1, :b => [4, 5, 3])
r = robject(d)
RObject{VecSxp}
$a
[1] 1

$b
[1] 4 5 3
# R -> Julia
rcopy(r)
DataStructures.OrderedDict{Symbol,Any} with 2 entries:
  :a => 1
  :b => [4, 5, 3]

Date

# Julia -> R
d = Date(2012, 12, 12)
r = robject(d)
RObject{RealSxp}
[1] "2012-12-12"
# R -> Julia
rcopy(r)
2012-12-12

DateTime

# julia -> R
d = DateTime(2012, 12, 12, 12, 12, 12)
r = robject(d)
RObject{RealSxp}
[1] "2012-12-12 12:12:12 UTC"
# R -> Julia
rcopy(r)
2012-12-12T12:12:12

DataFrames

d = DataFrame([[1.0, 4.5, 7.0]], [:x])
# Julia -> R
r = robject(d)
RObject{VecSxp}
    x
1 1.0
2 4.5
3 7.0
# R -> Julia
rcopy(r)
3×1 DataFrames.DataFrame
│ Row │ x   │
├─────┼─────┤
│ 1   │ 1.0 │
│ 2   │ 4.5 │
│ 3   │ 7.0 │

In default, the column names of R data frames are sanitized such that foo.bar would be replaced by foo_bar.

rcopy(R"data.frame(a.b = 1:3)")
3×1 DataFrames.DataFrame
│ Row │ a_b │
├─────┼─────┤
│ 1   │ 1   │
│ 2   │ 2   │
│ 3   │ 3   │

To avoid the sanitization, use sanitize option.

rcopy(R"data.frame(a.b = 1:10)"; sanitize = false)
10×1 DataFrames.DataFrame
│ Row │ a.b │
├─────┼─────┤
│ 1   │ 1   │
│ 2   │ 2   │
│ 3   │ 3   │
│ 4   │ 4   │
│ 5   │ 5   │
│ 6   │ 6   │
│ 7   │ 7   │
│ 8   │ 8   │
│ 9   │ 9   │
│ 10  │ 10  │

AxisArrays

# Julia -> R
aa = AxisArray([1,2,3], Axis{:id}(["a", "b", "c"]))
r = robject(aa)
┌ Warning: `indices(a)` is deprecated, use `axes(a)` instead.
│   caller = Type at core.jl:562 [inlined]
└ @ Core ~/.julia/packages/AxisArrays/uZ7JS/src/core.jl:562
RObject{IntSxp}
id
a b c
1 2 3
# R -> Julia
rcopy(AxisArray, r)
┌ Warning: `indices(a)` is deprecated, use `axes(a)` instead.
│   caller = Type at core.jl:562 [inlined]
└ @ Core ~/.julia/packages/AxisArrays/uZ7JS/src/core.jl:562
3-element AxisArrays.AxisArray{Int64,1,Array{Int64,1},Tuple{AxisArrays.Axis{:id,Array{String,1}}}}:
 1
 2
 3