Fork me on GitHub
#datascript
<
2022-01-07
>
grumplet10:01:03

Would anyone here have any pointers as to the best way to interop with R. I have a cljs implementation of an R coxph model and I need to write a coverage test that checks the cljs against the R code?

refset18:01:00

Hey, I have no idea what would actually work best in your situation, but I did just discover this possibility of calling graaljs (e.g. using compiled cljs) from this Truffle-based R-Graal implementation: https://github.com/oracle/fastr/blob/1ce6f36bec5c5f8520ab0f0f330f9dee23ed7c55/documentation/user/Interoperability.md Out of interest, are you using DataScript in the cljs as the underlying engine for the regression model? Or is it just one part of the picture?

zane16:01:19

You could also consider, as a first pass, avoiding interop entirely. Might not be worth it. Could either shell out to R from CLJS or have separate R and CLJS programs that write the results of the test expressions to disk and then have a third program examine and compare the results.

✔️ 1
1
jlongster18:01:42

hello! I’m trying to load in a serialized db using the JS API and can’t get it to work. I have the database in EDN format. I’ve tried several things, but this is the latest:

let ds = require('datascript');
let edn = require('jsedn');

let d = require('fs').readFileSync('./persona.edn', 'utf8');
let data = edn.parse(d);

let conn = ds.conn_from_datoms(
  edn.toJS(edn.atPath(data, ':datoms')),
  edn.toJS(edn.atPath(data, ':schema'))
);

let query = '[:find ?id :where [?b :block/uid ?id]]';
let result = ds.q(query, conn);

console.log(result);
conn is created fine but when running the query I get [object Object] is not ISeqable. Looking at the code, it doesn’t seem to run js->clj on the value of the datom, is that the problem? I can’t find any examples for doing this through JS

lilactown18:01:13

i think you need to get the current db from the conn

lilactown18:01:44

let result = ds.q(query, ds.db(conn));

lilactown18:01:43

translated to clojure:

(def result (ds/q query @conn))

jlongster18:01:08

nice! that does get a lot closer

jlongster18:01:06

it runs but the query errors with Cannot compare :block/parents to :block/refs. I know this might be specific to my db, but that query works from the place I exported the db from. I’m not referencing :block/parents at all — any ideas where I should look?

jlongster18:01:44

might be the schema

lilactown19:01:03

i'm getting similar errors with pretty basic data/schema

lilactown19:01:56

it works when i use string keys

lilactown19:01:18

let ds = require('datascript');
let edn = require('jsedn');

let d = require('fs').readFileSync('./persona.edn', 'utf8');
let data = edn.parse(d);

// let schema = edn.toJS(edn.atPath(data, ':schema'));
let schema = {"person/id": {":db/unique": ":db.unique/identity"}};

//let datoms = edn.toJS(edn.atPath(data, ':datoms'));
let datoms = [[1, "person/id", 1],
              [1, "person/name", "Will"]];

let conn = ds.conn_from_datoms(
    datoms, schema
);

let query = '[:find ?v :where [?p "person/id" ?v]]';
let result = ds.q(query, ds.db(conn));

console.log(result);

lilactown19:01:31

hmm i see. datascript does not convert strings to keywords other than ds-specific strings like :db/id and schema definition keywords

lilactown19:01:16

jsedn toJS stringifies keywords.

lilactown19:01:37

so you'll need to wrap your keyword attrs you're loading from disk in quotes in your query

lilactown19:01:01

i.e.

let query = '[:find ?id :where [?b ":block/uid" ?id]]';

lilactown19:01:58

not the nicest but unless you're going to read the EDN in as actual CLJS data yourself that might be as good as you get

lilactown19:01:49

also not sure what will happen if you try and round trip the db, i.e. make changes to the db and then write it back out to disk

jlongster19:01:01

very nice that works, thank you!

jlongster19:01:09

I don’t need changes, my project is just read-only for this 🙂

jlongster19:01:41

is there any other way to provide a query? I’m confused about this writeup: https://github.com/kristianmandrup/datascript-tutorial/blob/master/javascript_api.md#jsedn-converter

jlongster19:01:44

is that old?

lilactown20:01:00

I think the key their is they're parsing the query string using edn.parse

lilactown20:01:41

so that turns it into JS data. then datascript reads that JS data, and treats the attributes it finds as strings

lilactown20:01:56

so in your case that would work well because you're parsing the datums into JS objs and then you could parse the query string as well

jlongster20:01:32

yeah, thanks! I’ve tried it though and something is weird. why do that pass query as an array? d.q([query], …)

jlongster20:01:11

I’ve tried let result = ds.q(edn.parse(query), ds.db(conn)); which gives No protocol method ICounted.-count defined

jlongster20:01:34

and if I wrap it in an array, this.ja.charAt is not a function

lilactown20:01:37

yeah that is weird. if you note in that example they do not wrap the query string in a vector, it's just kws and variables

lilactown20:01:09

maybe that example is just out of date

jlongster20:01:31

it’s from 2016, so likely