This article outlines how to access and use JavaScript in Displayr.
Method
JavaScript is the scripting language of Displayr. It is used for:
- Creating numeric or text variables:
- from the Data Sources tree by hovering over any variable > Plus (+) > Custom Code > JavaScript > Numeric / JavaScript > Text.
- from the toolbar via Anything
> Data > Variables > New > Custom Code > JavaScript > Numeric / JavaScript > Text. See How to Create a Custom Numeric JavaScript Variable and How to Create a Custom JavaScript Text Variable.
- Creating Rules:
- from the Data tab in the object inspector via Rules > Plus (+) > New custom rule (write your own JavaScript) > Edit JavaScript. See How to Create a Custom Rule.
- Creating QScripts (for Enterprise users):
- from the toolbar via Tools
> QScript > New. See How to Create a Custom QScript.
- from the toolbar via Tools
- Creating input fields that show in the object inspector in Edit Mode for R-based analysis tools:
- from the Data tab in the object inspector via Inputs Javascript. See How to Create User Input Fields in a Custom Analysis Tool.
- Writing many of the functions available from in-app menus.
JavaScript versus R
- Just like R variables, JavaScript variables are updated automatically in the project, and you can also easily go back to edit variables if you want to tweak the code.
- JavaScript code is run through your browser and was originally designed as a scripting language for websites. R code, on the other hand, requires data to be packaged up and processed on our R servers and was specifically designed to do basic as well as advanced analysis.
- While usually there isn't a noticeable difference between creating a basic JavaScript variable and an R variable, JavaScript variables are processed more efficiently than R. But when your data set has million+ cases, R can be faster because it can vectorize calculations instead of needing to loop through each individual observation. For very large data sets, selecting a JavaScript variable and in the object inspector, checking Data > Javascript Code > Accelerate (skip logic checks) will significantly improve the speed of JavaScript variables.
- Some advanced calculations may be easier - or only possible - to create using R.
- JavaScript can only reference the Name of a variable, whereas R can reference the Name or Label.
- JavaScript can only reference a single Variable at a time, whereas R can reference the Variable Set.
- JavaScript is 0-based so the first iteration in a loop or array is 0, whereas in R it is 1.
- JavaScript variables and Table Rules cannot reference other items in the Report tree.
- When copying a variable over from a linked data set, it is often better to use JavaScript.
Displayr specific JavaScript functions
While all the standard functions are available in JavaScript, we have also created our own set of functions:
-
isNaN(variable_name)
- Returns a 0 if the variable is not missing and a 1 if the variable is missing. -
Q.AsNumeric(variable_name)
- Converts a Text Variable containing numbers into a Numeric Variable. -
Q.Label(variable_name)
- Converts a variable's value into its corresponding label. e.g. for a Gender question, 2 would become Female. -
Q.Source(variable_name)
- Returns the original source value of a variable, ignoring any recoding or Exclude from Analyses settings.
Next
How to Use JavaScript in Displayr
How to Work with Conditional JavaScript Formulas
How to Work with JavaScript Arrays and Loops
How to Create JavaScript Variables Using Loops and Arrays
How to Troubleshoot JavaScript Code in Displayr
How to Create a Custom Numeric JavaScript Variable
How to Create a Custom JavaScript Text Variable