-
Notifications
You must be signed in to change notification settings - Fork 59
/
default.jl
271 lines (233 loc) · 6.86 KB
/
default.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# logic for default rcopy
"""
`rcopy(r)` copies the contents of an R object into a corresponding canonical Julia type.
"""
rcopy(r::RObject{S}; kwargs...) where S<:Sxp = rcopy(r.p; kwargs...)
function rcopy(s::Ptr{S}; kwargs...) where S<:Sxp
protect(s)
try
for class in rcopy(Array{Symbol}, getclass(s))
T = rcopytype(RClass{class}, s)
if T != RObject
return rcopy(T, s; kwargs...)
end
end
T = rcopytype(RClass{:default}, s)
return rcopy(T, s; kwargs...)
finally
unprotect(1)
end
end
# Default behaviors of copying R vectors to AbstractArray
function rcopy(::Type{AbstractArray}, s::Ptr{S}) where S<:Sxp
protect(s)
try
if isFactor(s)
return rcopy(CategoricalArray, s)
else
return rcopy(Array, s)
end
finally
unprotect(1)
end
end
# Default behaviors of copying R vectors to arrays
for S in (:IntSxp, :RealSxp, :CplxSxp, :LglSxp, :StrSxp, :RawSxp)
@eval begin
function rcopy(::Type{Vector},s::Ptr{$S})
protect(s)
try
for class in rcopy(Array{Symbol}, getclass(s))
T = rcopytype(RClass{class}, s)
if T != RObject
return rcopy(Vector{eltype(RClass{class}, s)}, s)
end
end
return rcopy(Vector{eltype(RClass{:default}, s)}, s)
finally
unprotect(1)
end
end
function rcopy(::Type{Array},s::Ptr{$S})
protect(s)
try
for class in rcopy(Array{Symbol}, getclass(s))
T = rcopytype(RClass{class}, s)
if T != RObject
return rcopy(Array{eltype(RClass{class}, s)}, s)
end
end
return rcopy(Array{eltype(RClass{:default}, s)}, s)
finally
unprotect(1)
end
end
end
end
# NilSxp
rcopy(::Ptr{NilSxp}) = nothing
# SymSxp and CharSxp
rcopy(s::Ptr{SymSxp}) = rcopy(Symbol,s)
rcopy(s::Ptr{CharSxp}) = rcopy(String,s)
# StrSxp
function rcopytype(::Type{RClass{:default}}, s::Ptr{StrSxp})
if anyna(s)
length(s) == 1 ? Missing : Array{Union{String, Missing}}
else
length(s) == 1 ? String : Array{String}
end
end
function eltype(::Type{RClass{:default}}, s::Ptr{StrSxp})
if anyna(s)
Union{String, Missing}
else
String
end
end
function rcopytype(::Type{RClass{:default}}, s::Ptr{IntSxp})
if isFactor(s)
CategoricalArray
else
if anyna(s)
length(s) == 1 ? Missing : Array{Union{Int, Missing}}
else
length(s) == 1 ? Int : Array{Int}
end
end
end
function eltype(::Type{RClass{:default}}, s::Ptr{IntSxp})
if anyna(s)
Union{Int, Missing}
else
Int
end
end
function rcopytype(::Type{RClass{:default}}, s::Ptr{RealSxp})
if anyna(s)
length(s) == 1 ? Missing : Array{Union{Float64, Missing}}
else
length(s) == 1 ? Float64 : Array{Float64}
end
end
function eltype(::Type{RClass{:default}}, s::Ptr{RealSxp})
if anyna(s)
Union{Float64, Missing}
else
Float64
end
end
function rcopytype(::Type{RClass{:default}}, s::Ptr{CplxSxp})
if anyna(s)
length(s) == 1 ? Missing : Array{Union{ComplexF64, Missing}}
else
length(s) == 1 ? ComplexF64 : Array{ComplexF64}
end
end
function eltype(::Type{RClass{:default}}, s::Ptr{CplxSxp})
if anyna(s)
Union{ComplexF64, Missing}
else
ComplexF64
end
end
function rcopytype(::Type{RClass{:default}}, s::Ptr{LglSxp})
if anyna(s)
length(s) == 1 ? Missing : Array{Union{Bool, Missing}}
else
length(s) == 1 ? Bool : BitArray
end
end
function eltype(::Type{RClass{:default}}, s::Ptr{LglSxp})
if anyna(s)
Union{Bool, Missing}
else
Bool
end
end
function rcopytype(::Type{RClass{:default}}, s::Ptr{RawSxp})
if anyna(s)
length(s) == 1 ? Missing : Array{Union{UInt8, Missing}}
else
length(s) == 1 ? UInt8 : Array{UInt8}
end
end
function eltype(::Type{RClass{:default}}, s::Ptr{RawSxp})
if anyna(s)
Union{UInt8, Missing}
else
UInt8
end
end
# VecSxp
function rcopytype(::Type{RClass{:default}}, s::Ptr{VecSxp})
if isFrame(s)
DataFrame
elseif isnull(getnames(s))
Array{Any}
else
OrderedDict{Symbol,Any}
end
end
# FunctionSxp
rcopytype(::Type{RClass{:function}}, s::Ptr{S}) where S<:FunctionSxp = Function
# LangSxp
rcopytype(::Type{RClass{:call}}, l::Ptr{LangSxp}) = Expr
rcopytype(::Type{RClass{Symbol("(")}}, l::Ptr{LangSxp}) = Expr
rcopytype(::Type{RClass{:formula}}, l::Ptr{LangSxp}) = FormulaTerm
# Fallback
rcopytype(::T, s::Ptr{S}) where {T, S<:Sxp} = RObject
# Fallback for non SEXP
rcopy(r) = r
# logic of default sexp
"""
`robject(x)` converts a Julia object `x` to a corresponding RObject implicitly. Explicit conversions
could be called with `robject(<R Class>, x)`.
"""
robject(s) = RObject(s)
RObject(s) = RObject(sexp(s))
"""
`sexp(x)` converts a Julia object `x` to a pointer to a corresponding Sxp Object.
"""
sexp(s) = sexp(sexpclass(s), s)
# Nothing / Missing
sexpclass(::Nothing) = NilSxp
sexpclass(::Missing) = RClass{:logical}
# Symbol
sexpclass(s::Symbol) = SymSxp
# DataFrame
sexpclass(d::AbstractDataFrame) = RClass{:list}
# Number, Array
for (J, C) in ((:Integer,:integer),
(:AbstractFloat, :numeric),
(:Complex, :complex),
(:Bool, :logical),
(:AbstractString, :character),
(:UInt8, :raw))
@eval begin
sexpclass(v::$J) = RClass{$(QuoteNode(C))}
sexpclass(a::AbstractArray{Union{T, Missing}}) where T<:$J = RClass{$(QuoteNode(C))}
sexpclass(a::AbstractArray{T}) where T<:$J = RClass{$(QuoteNode(C))}
end
end
# Fallback: entire column of missing to NA
# R assigns these to logical by default, although if it's all missing, it doesn't matter much
sexpclass(a::AbstractArray{Missing}) = RClass{:logical}
# Fallback: convert AbstractArray to VecSxp (R list)
sexpclass(a::AbstractArray) = RClass{:list}
# AbstractDict
sexpclass(d::AbstractDict) = RClass{:list}
# Function
sexpclass(f::Function) = RClass{:function}
# LangSxp
sexpclass(f::FormulaTerm) = RClass{:formula}
# Date
sexpclass(d::Date) = RClass{:Date}
sexpclass(d::AbstractArray{Union{Date, Missing}}) = RClass{:Date}
sexpclass(d::AbstractArray{Date}) = RClass{:Date}
# DateTime
sexpclass(d::DateTime) = RClass{:POSIXct}
sexpclass(d::AbstractArray{Union{DateTime, Missing}}) = RClass{:POSIXct}
sexpclass(d::AbstractArray{DateTime}) = RClass{:POSIXct}
# CategoricalArray
sexpclass(v::CategoricalArray) = RClass{:factor}
sexpclass(v::SubArray{<:Any, <:Any, <:CategoricalArray}) = RClass{:factor}