Skip to content

Commit

Permalink
base: new S3 module, access to underlying R value
Browse files Browse the repository at this point in the history
  • Loading branch information
pveber committed Nov 21, 2018
1 parent 650d50d commit a76a361
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
24 changes: 14 additions & 10 deletions lib/base/OCamlR_base.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
open OCamlR

type any
open OCamlR_wraputils

module Stubs = OCamlR_base_stubs
module Stubs2 = OCamlR_base_stubs2
Expand All @@ -14,8 +13,17 @@ let subset_ii x i j = Stubs2.subset_ii x (R.int i) (R.int j)
let subset2_i x i = Stubs2.subset2_i x (R.int i)
let subset2_s x s = Stubs2.subset2_s x (R.string s)

module S3 = struct
type t = t R.t
let r x = x
let _class_ =
let symbol = R.symbol "class" in
fun (x : t) ->
R.strings_of_t (R.eval symbol [ R.arg ident x ])
end

module Environment = struct
type t = any R.t
include S3
let create () = Stubs.new'env ()
let unsafe_get env ~class_ x =
let y = Stubs2.subset2_s env (R.string x) in
Expand All @@ -25,20 +33,16 @@ module Environment = struct
else None
end

module Dataframe_common = struct
module Dataframe = struct
include S3

let dim x =
match Stubs.dim'data'frame ~x () |> R.ints_of_t with
| [| i ; j |] -> (i, j)
| _ -> assert false
end

module Dataframe = struct
type t = any R.t

let of_env env x =
Environment.unsafe_get env ~class_:"data.frame" x

include Dataframe_common
end

let sample ?replace ?prob ~size x =
Expand Down
10 changes: 10 additions & 0 deletions lib/base/OCamlR_base.mli
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@

open OCamlR

module S3 : sig
type t

val r : t -> t R.t
val _class_ : t -> string array
end

module Environment : sig
type t
include module type of S3 with type t := t

val create : unit -> t
(** wrapper for [new.env] *)
end

module Dataframe : sig
type t
include module type of S3 with type t := t

val of_env : Environment.t -> string -> t option
val dim : t -> int * int
end
Expand Down
2 changes: 1 addition & 1 deletion lib/base/dune
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(name OCamlR_base)
(public_name ocaml-r.base)
(wrapped false)
(libraries ocaml-r.interpreter))
(libraries ocaml-r.interpreter ocaml-r.wraputils))

(rule
(targets OCamlR_base_stubs.ml)
Expand Down

0 comments on commit a76a361

Please sign in to comment.