forked from fslaborg/RProvider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
converters.fs
39 lines (33 loc) · 1.5 KB
/
converters.fs
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
namespace RProviderConverters
open RDotNet
open RProvider
open System.ComponentModel.Composition
open System.Linq
// Contains higher-level converters
module Factor =
let getLevels sexp = (RInterop.call "base" "levels" [| sexp |] [| |]).AsCharacter().ToArray()
let tryConvert sexp =
match sexp with
| IntegerVector(nv) when sexp.Class = [| "factor" |] ->
Some( let levels = getLevels sexp
nv |> Seq.map (fun i -> levels.[i-1])
|> Seq.toArray )
| _ -> None
[<Export(typeof<IConvertFromR<string[]>>)>]
[<Export(typeof<IDefaultConvertFromR>)>]
type FactorVectorConverter() =
interface IConvertFromR<string[]> with
member this.Convert(sexp: SymbolicExpression) = tryConvert sexp
// Implementing this interface indicates that if we can convert
// the value, we are the default converter to use
interface IDefaultConvertFromR with
member this.Convert(sexp: SymbolicExpression) =
tryConvert sexp |> Option.map box
[<Export(typeof<IConvertFromR<string>>)>]
type DataFrameConverter() =
interface IConvertFromR<string> with
member this.Convert(sexp: SymbolicExpression) =
match sexp with
| IntegerVector(nv) when sexp.Class = [| "factor" |] && nv.Length = 1 ->
Some <| getLevels(sexp).[nv.[0]]
| _ -> None