07 December 2017 |
Filtering a Subset of Tables and Visualizations on a Page in Displayr
When you are working in Displayr’s edit mode, you can choose which items on a page to filter by selecting the items and applying a filter. When people view your published document (view mode) and apply a filter to a page, the default behavior is that all the things on the page are filtered. Sometimes you may want design your document in such a way that when your viewers apply a filter, it will affect some items but not others.
This post describes four strategies for restricting the filtering so that it only applies to a subset of the items. Strategies 1 and 2 are to create outputs which do not interact with the page filters. Such items will not update when a user changes the filters. Strategy 3 is to set up your own custom menus to control filters, rather than using the built-in Filters menu. Finally, Strategy 4 is to set up items which do not update at all until you, the document author, choose to update them.
Strategy 1: Creating R Outputs that do not use filters
The first strategy is to use an calculation in an R Output to generate the desired result. As R calculations do not incorporate filters unless you deliberately build the filter in to the calculation, such an output will be unaffected when a view of your document applies a filter. Consider the following example of creating a table with R.
The table below on the left was created by dragging a variable from the Data tree onto a page. If a filter is applied in view mode this table will be automatically updated. The table on the right was created by inserting an R Output with R CODE of
cbind("%" = prop.table(table(Q3)) * 100)
and formatting the percent sign and decimals via the Appearance tab. It will not update when a viewer applies a filter, as the R CODE does not provide any instructions about what to do in the event of a filter. If the code was instead
cbind("%" = prop.table(table(Q3[QFilter])) * 100)
it would automatically be updated when a filter was applied.
Strategy 2: Removing QFilter from automatically-created R Ouptuts
The second strategy (which applies to items in Create > Visualization and in Create > More) is to remove the built-in recognition of filters from these outputs. Once removed, the outputs will not respond to filters applied to the page.
Most advanced analyses and visualizations in R are automatically-generated R Outputs. In edit mode, you can access their underlying code by clicking on the output and looking at Properties > R CODE in the Object Inspector. For example, the output below shows the R CODE of a regression model. When you look at the code, you will see that green formatting highlights where the code accesses any weights (QPopulationWeight) and filters (QFilter).
If you edit the code, and replace QFilter with TRUE the code will ignore any filters. If you wish to add any filters to the code you can do so here as well; for example, replacing QFilter with gender == “Female” would cause the analyses to be based on data with a value of “Female” for the gender variable.
Strategy 3: Creating custom filters via controls
The automated filtering in Displayr view mode allows the user to apply filters by clicking on the Filters link at the top of the page. Alternatively, custom filter controls can be placed on a page and these can be selectively linked to different outputs. See How to Create an Interactive Infographic for a worked example.
Strategy 4: R Outputs with ‘Automatic’ unchecked
R Outputs only update if the Automatic checkbox at the top of the Object Inspector ticked (as it is in the example above). Thus, any R Output can be prevented from being filtered by unchecking this. Of course, if you update your data then the output will not update to reflect this, so this strategy is dangerous. You would need to remember to manually update the outputs in your document when the data is updated.
This strategy can also be applied to tables created by dragging from the Data Tree, as follows:
- Create the table
- Hide the table (Appearance > Hide), so that it does not appear in your published document
- Look up the Name of the table by clicking in Properties > GENERAL in the Object Inspector
- Insert > R Output and set the R CODE to the name of the table (e.g., table.Q2) and press Calculate
- Uncheck Automatic
This is effectively using R to make a copy of the table. By unchecking Automatic, the table will remain in this state until you go in and update it manually with Calculate .
Author: Tim Bock
Tim Bock is the founder of Displayr. Tim is a data scientist, who has consulted, published academic papers, and won awards, for problems/techniques as diverse as neural networks, mixture models, data fusion, market segmentation, IPO pricing, small sample research, and data visualization. He has conducted data science projects for numerous companies, including Pfizer, Coca Cola, ACNielsen, KFC, Weight Watchers, Unilever, and Nestle. He is also the founder of Q www.qresearchsoftware.com, a data science product designed for survey research, which is used by all the world’s seven largest market research consultancies. He studied econometrics, maths, and marketing, and has a University Medal and PhD from the University of New South Wales (Australia’s leading research university), where he was an adjunct member of staff for 15 years.