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 using R code. A Calculation can also be referred to by other Calculations and Variables constructed using R code.
Requirements
A Displayr document.
Please note this requires the Data Stories module or a Displayr license.
Method
1. In the toolbar, click Calculation > Custom Code.
2. Click on the page where you wish to place the calculation.
3. Enter your R code in the R Code editor above the page.
4. Click Calculate in the object inspector.
5. Enter a name under General > Name.
5. OPTIONAL: You can adjust the display options under the Data > Output tab:
- 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 want your Calculation to automatically re-calculate when the data updates, untick Calculate automatically.
8. OPTIONAL: If you don't want to have this Calculation visible when your document is published, tick General > Hidden from exports & publishing.
9. OPTIONAL: When outputting respondent-level data that matches the number of records in your data set, you can additionally apply a filter via Data > 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.
Next
How to use the Filter(s) Dropdown to Quickly Filter R Outputs Using QFilter
How to Filter Raw Data Using R