odin.js

Project Status: WIP – Initial development is in progress, but there has not yet been a stable, usable release suitable for the public. R build status codecov.io

knitr::opts_chunk$set(error = FALSE)

Support for compiling odin models to javascript.

Use from R

The function odin.js::odin_js provides a (roughly drop-in) replacement for odin::odin for compiling an odin model for use within R:

logistic <- odin.js::odin_js({
  deriv(N) <- r * N * (1 - N / K)
  initial(N) <- N0
  N0 <- user(1)
  K <- 100
  r <- user()
})
logistic
logistic(r = 1)$run(0:10)

This might be slightly faster than the odin-to-R compiler, though benchmarks will have to wait until odin.js supports more odin features.

Use from webpages

The function odin.js::odin_js_bundle generates a "bundle" of javascript suitable for calling from a webpage. This is the same code that odin.js::odin_js but without R-wrappers around it. It takes a vector of filenames and produces a file of javascript code:

path <- odin.js::odin_js_bundle("inst/models/logistic.R",
                                include_dopri = FALSE)

The file at path now contains:

cat(c("```js", readLines(path), "```"), sep = "\n")

(the option include_dopri prevents the dopri-js solver being included here as that can be included separately and ends up being ~500 lines of extra code).

Complete example

A more complete example that can actually be used from a webpage is included:

path <- odin.js::odin_js_example("inst/models/sir.R", "simple")
dir(path)

Chrome will not let you execute js directly off disk (though I think Firefox does) so to test this you should use something like SimpleHttpServer to serve the directory and access that way. This example is also available at mrc-ide.github.io/odin.js/simple

Supported features

Limitations and differences


Please note that the 'odin.js' project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.



mrc-ide/odin.js documentation built on Nov. 2, 2022, 2:27 p.m.