Text Variables can be created as JavaScript Variables or using Excel-Style Formulas, selecting Text rather than Numeric when creating the variables.
The only difference between creating Text Variables and creating Numeric Variables is that one returns a string of text whereas the other returns either a number or a NaN.
Requirements
- Comfort with JavaScript code. See JavaScript Fundamentals if you're new to JavaScript or new to working with it in Displayr.
Please note these steps require a Displayr license.
Method
1. Either hover over any variable in your Data Sources tree > Plus (+) > Insert Variable(s) > Custom Code > JavaScript - Text, or else from the toolbar menu, select the icon and select Data > Variables > New > Custom Code > JavaScript - Text.
2. Enter your JavaScript code into the JAVASCRIPT CODE section in the object inspector on the right.
3. Click the Calculate button to run the code and create the JavaScript variable.
By default, a new variable will be added to the top of your Data Sources tree on the left. Note, if you first select a variable in your Data Sources tree, the R variable will be added directly after that. Additionally, if you have multiple data sets in your document, you should first select the appropriate Data Sources tree you wish to add the variable to.
4. Update Name and Label as appropriate.
5. OPTIONAL: If you wish to change your variable to categorical, set Structure to Nominal. This will then use the text results as category labels.
6. OPTIONAL: Tick Access all data rows if you wish to work with arrays. The result must also be an array.
7. OPTIONAL: Tick 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;
.