This article describes how to add a custom code Calculation to your document. A Calculation in Displayr uses R code. Custom code could be used to create virtually anything, and is typically used to output a table, chart, data set, text string, or an error message. These outputs can in turn only be modified via R. A Calculation can also be referred to by other Calculations and Variables constructed using R.
A Displayr document.
1. In the toolbar, click on the Calculation = icon or select Calculation > Custom Code.
2. Click on your page where you wish to have this Calculation inserted, and drag to create a box for the output.
3. Enter a name under GENERAL > Name.
4. Enter your R code in the object inspector under Properties > R CODE.
5. OPTIONAL: You can adjust the display options under OUTPUT:
- Show as lets you choose between text and table (when appropriate).
- Include R code lets you display the R code with the result.
- Show raw R output prints your results with additional R messages and logs, and can be used for returning the results of intermediate objects.
6. OPTIONAL: Under APPEARANCE, you can adjust the decimal places and number format.
7. OPTIONAL: If you don't wish for your Calculation to automatically re-calculate when the data updates, untick Automatic.
8. OPTIONAL: If you wish to not have this Calculation visible when your document is published, tick Hidden from exported views.
9. OPTIONAL: When outputting respondent-level data that matches the number of records in your data set, you can additionally apply a filter via Inputs > FILTERS & WEIGHT > Filter(s).
Note, this will only work if you use
QFilter in your R code to filter your rows. Otherwise, you will get the following warning:
If you wish to apply filters to aggregated tables referenced in your code, or even a Standard R output such as a visualization, you will instead need to apply them to your source tables so your data can be filtered before aggregation.