Requirements
- Comfort with JavaScript code. See JavaScript Fundamentals if you're new to JavaScript or new to working with it in Displayr.
Method
1. Hover over any variable in your Data Sources tree and click Plus (+) > Insert Variable(s) > Custom Code > JavaScript - Numeric.
2. Enter your JavaScript code into the code editor window.
3. Click the Calculate button to run the code and create the JavaScript variable.
Note, if you first selected a variable in your Data Sources tree, the new variable will be added directly after that.
4. On the General tab, update Name and Label as appropriate.
5. OPTIONAL: If you wish to output a categorical variable, change Data > Properties > Structure to Nominal.
You can then update the labels by selecting Data > Properties > Labels:
6. OPTIONAL: If the result is binary (0 or 1) and you wish to use this variable as a filter, tick Usable as a filter.
7. OPTIONAL: If the result is numeric and you wish to use this variable as a weight, tick Usable as a weight.
8. OPTIONAL: Tick Data > JavaScript Code > Access all data rows if you wish to work with arrays. The result must also be an array.
9. OPTIONAL: Tick Data > JavaScript Code > Accelerate (skip logic checks) to run your variable faster, particularly when your code is simple. In this case, you must first ensure you initialize all local variables before use. For example, write var sum = 0;
instead of var sum;
.