If your Calculation uses variables or a raw data table as an input (as opposed to a table/visualization/other output), you can use the standard Filter(s) dropdowns to quickly filter that data for your R Calculation. This is useful when you have a custom R Calculation that should be recalculated based on filtering changed by a combo box (or other Control). Instead of having to build out the Control filtering logic inside the R code, you can simply use the variable QFilter
in your code and apply the filtering variable to the output, like you would a built-in drag and drop table. This article describes how the QFilter
object can be used within your R code to apply filters to case-level data.
Requirements
- A Calculation that uses variables from the data set or an unfiltered raw data table.
- One or more filter variables from the same data set.
Method
1. In the toolbar or right click menu in the Report pane, go to Calculation > Custom Code. To create a blank R Calculation output.
2. In this example, we will filter the values of a variable Age
by a filter(s) applied in the Filter(s) dropdown (referenced by QFilter
in the code). The Filter(s) field doesn't automatically show in the object inspector, you must first add QFilter
to your code, and then the field will appear. In the Code Editor at the top type the following:
Age[QFilter]
As this is a single variable, we simply need to place QFilter
within square brackets. If your data is for multiple variables that are combined in a data.frame() or raw.data table, then the reference needs a comma to denote the QFilter
should filter the rows of the raw data not the columns, i.e. [QFilter,]
.
3. Now the Data > Filters & Weight > Filter(s) appears in the object inspector where you can apply the desired filter variable(s). If you apply multiple filters, they will be combined like on normal tables into the single QFilter
variable (see Technical details below). Here, we will apply a Female filter:
The Age variable had 800 cases in it originally and now the Calculation returns 405 records, the same number as females in our data set.
Technical details
- If a filter(s) is selected in the Filter(s) dropdown,
QFilter
will be a series ofTRUE
andFALSE
values as many cases as is in your data set. You can then use this to filter the raw data/variables being used in your calculation using this variable. - If multiple filters are selected,
QFilter
will return with a final series ofTRUE
andFALSE
values evaluated by combining the filters. Filters are combined using AND for categories in separate variable sets and OR for categories within the same variable set, i.e. if filtering on Gender and Age, with male and under 18 and 18-24 categories selected, the final values will reflect cases that are male AND (under 18 OR 18-24). - The series of
TRUE
andFALSE
values automatically update when the filters change in the dropdown. - If no filters are applied, it is a single value of
TRUE
. - If you apply filters to your Calculation via Data > Filters & Weight > Filter(s) but do not include
QFilter
in your code, you will receive the following warning: - The Variable Label is available as an attribute (i.e.,
attr(QFilter, "label")
). Where there are multiple filters, they are concatenated as a list (e.g., "Male, 18 to 24 and Tall"). - The Variable Name is available as an attribute (i.e.,
attr(QFilter, "name")
). Where there are multiple names, they are concatenated as with labels.
Next
How to Add a Custom Calculation
How to Work with R in Displayr