molgenis / molgenis-js-magma

MagmaScript evaluator in JavaScript
GNU Lesser General Public License v3.0
0 stars 4 forks source link

I would like to use this in a JS session provided by the R package V8 #27

Open tombisho opened 3 years ago

tombisho commented 3 years ago

I would be interested to know how difficult it would be to provide the magmajs behaviour in a JavaScript engine session running within an R session.

Background:

The magmajs editor in MOLGENIS or Opal is fine for simple harmonisation. It is a good solution for harmonising as it can be used without the user having full access to the data. However using it for complex harmonisation can be challenging to debug and spot problems. We have been experimenting with generating synthetic data on the client side, which is a realistic representation of the real data, and that the user can have full access to. We then tried loading this into DSLite and using DataSHIELD functions to harmonise, which proved difficult because of the limited functions in DataSHIELD. Therefore the next idea is to try to use the V8R package, which provides JavaScript in R. Then, the synthetic data can be put into the JavaScript session. JavaScript code can be written to do the harmonisation, and the user can have full access to data and see how things are working. When they are happy, the user can cut and paste the finished JS code into the MOLGENIS/Opal editor to run on the real data. Question:

magmajs introduces features that are not available in standard JS. For example in magmajs we would refer to a column with the syntax $("smoking"). In V8, we can pull a dataset into JS but would refer to the columns as my_data.smoking.

V8 does offer the chance to pull JS libraries in, so we wondered if it is possible to pull the magmajs functionality for use with V8. This would mean the code written on the client side with full access to the synthetic data could be dropped into the MOLGENIS/Opal editor unchanged.

tombisho commented 3 years ago

I have managed to build the MagmaScript library with the following steps:

 git clone https://github.com/molgenis/molgenis-js-magma.git
 cd molgenis-js-magma/
 yarn install
 yarn build

I can now get the MagmaScript module to load in R V8:

ct_new$source("/home/vagrant/molgenis-js-magma/dist/MagmaScript.min.js")

I can also load some data into the session:

ct_new$assign("diamonds", diamonds)

But I am stuck what to do next. I know I must somehow get the $ notation up and running. I have created another bit of dummy data:

 const entity = {
    'id': 'A',
    'myStringAttributeName': 'abcde0123',
    'myIntAttributeName': 4,
    'height': 180,
    'name': 'caroline',
    'hasEars': true,
    'male': false,
    'female': true,
    'pregnant': true,
    'data': 1,
    'gender': {'_idValue': 'm', id: 'm', 'label': 'Male'}
  }

and it seems I need to do something like:

const $ = MagmaScript.$.bind(entity)

but it throws:

TypeError: Cannot call method 'bind' of undefined

Any help would be greatly appreciated!