SciNim / rnim

A bridge between R and Nim

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Autogenerate a wrapper R module for Nim generated .so

Vindaar opened this issue · comments

Currently when using Nim to write a shared library to be used from R, we still have to write essentially a small wrapper module to wrap the functions in the shared library using .Call. For example:

foo.nim:

proc addXY*(x, y: SEXP): SEXP {.exportc: "addXY", cdecl, dynlib.} =
  let
    xNim = x.to(float)
    yNim = y.to(float)
  result = nimToR(xNim + yNim)

After compilation in R we need something like:
callNim.R

dyn.load("libtNimFromR.so")
addXY <- function(a, b) {
    return(.Call("addXY", a, b))
}

As the syntax for this is always the same, we can easily autogenerate a corresponding R file with all functions wrapped in this manner.

Once we have a single {.exportR.} pragma and we allow for native Nim types in procedures for R, we can even add things like as.integer etc. to the arguments in the R wrapper.

Added in #10.