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 Vector{Float64}:
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)
OrderedCollections.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
Note that R's POSIXct
supports higher precision than DateTime:
r = reval("as.POSIXct('2020-10-09 12:09:46.1234')")
RObject{RealSxp}
[1] "2020-10-09 12:09:46 UTC"
d = rcopy(r)
2020-10-09T12:09:46.123
POSIXct
stores times internally as UTC with a timezone attribute. The conversion to DateTime
necessarily strips away timezone information, resulting in UTC values.
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 DataFrame
Row | x |
---|---|
Float64 | |
1 | 1.0 |
2 | 4.5 |
3 | 7.0 |
In default, the column names of R data frames are normalized such that foo.bar
would be replaced by foo_bar
.
rcopy(R"data.frame(a.b = 1:3)")
3×1 DataFrame
Row | a_b |
---|---|
Int64 | |
1 | 1 |
2 | 2 |
3 | 3 |
To avoid the normalization, use normalizenames
option.
rcopy(R"data.frame(a.b = 1:10)"; normalizenames = false)
10×1 DataFrame
Row | a.b |
---|---|
Int64 | |
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)
RObject{IntSxp}
id
a b c
1 2 3
# R -> Julia
rcopy(AxisArray, r)
1-dimensional AxisArray{Int64,1,...} with axes:
:id, ["a", "b", "c"]
And data, a 3-element Vector{Int64}:
1
2
3