Model Scripting
There are several options to programmatically script Insight Maker models.
In-Page API
Insight Maker has an in-page API that lets you script and take control of a model using JavaScript. A list of the functions in this API is available here.
You can add a Button primitive to access the in-page API. Each Button has an action which is JavaScript code that may call API commands. When the Button primitive is clicked by the user, its action is run. You can also trigger the in-page API via an action in Storytelling mode.
NPM Package
The simulation NPM package allows you to run and modify Insight Maker models in Node.js and also in the browser.
You can install the simulation NPM package with:
npm install --save simulation
After installing, here is an example where we construct and simulate a simple population growth model in JavaScript:
import { Model } from "simulation";
let m = new Model({
timeStart: 2020,
timeLength: 100,
timeUnits: "Years"
});
// Start with 7 billion people in the "people" stock
let people = m.Stock({
name: "People",
initial: 7e9
});
// Use a net growth rate of 2% a year
let growthRate = m.Variable({
name: "Growth Rate",
value: 0.02
});
// The population growth each year is the number of people times the growth rate
// Please note that we refer to the value of other primitives in the model with the
// [name] syntax.
let netGrowth = m.Flow(null, people, {
rate: "[People] * [Growth Rate]"
});
// For the netGrowth flow to be able to reference the growthRate, we need to link the primitives
m.Link(growthRate, netGrowth);
console.log(m.simulate());
You can learn more about the simulation NPM package here.
R Package
The sdbuildR package supports simulating Insight Maker models within R. Once installed, you can do something like:
sfm <- insightmaker_to_sfm(URL = "https://insightmaker.com/insight/43tz1nvUgbIiIOGSGtzIzj/Romeo-Juliet") sim <- simulate(sfm) plot(sim)
