Although Displayr automates the creation of the most commonly-required variables, sometimes there's a need to create a new variable using custom code. Usually, the best way to do this is to create a numeric R variable. You can also create multiple R variables if needed, see How to Create Multiple R Variables.
1. Either hover over any variable in your Data Sets tree > Plus (+) > Insert Variable(s) > Custom Code > R - Numeric, or else from the toolbar menu, select Anything > Data > Variables > New > Custom Code > R - Numeric.
2. Enter your R code into the R CODE section in the object inspector on the right.
Note, your R code must return the same number of results as records in your data set.
3. Click the Calculate button to run the code and create the R variable.
By default, a new variable will be added to the top of your Data Sets tree on the left. Note, if you first select a variable in your Data Sets 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 Sets tree you wish to add the variable to.
4. Update Name and Label as appropriate.
5. OPTIONAL: If you wish to output a categorical variable, change Structure to Nominal.
If you are simply referencing a categorical variable in your code, the labels will carry over when it is set to Nominal. However, you can also select DATA VALUES > Labels and update the labels there:
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.
For a more general overview of using R in Displayr, see the Displayr Help section on R.