This article describes how to reference table parameters within a Rule, such as the data set variables and variable sets that appear in the rows and columns of the table.
Requirements
- A table with a Rule applied.
- This article assumes you are already in the New custom rule (write your own JavaScript) > Edit JavaScript Rule view specified in How to Create a Custom Rule and have read this article.
Method
Referencing variables in a table
Following the naming conventions of Displayr’s sister product, Q, row variable sets are referred to as blue questions, and column variable sets are referred to as brown questions due to the input colors in Q’s interface.
In this example, the following would return the names of the input variable sets in our table:
table.blue // 'Row' variable set
table.brown // 'Column' variable setNote that if the table is not a crosstab, the latter will return Raw Data or Summary, depending on the table setup.
We can also access details about these variable sets by using blueQuestion and brownQuestion to find out their Structure, for example:
table.blueQuestion.variableSetStructure
table.brownQuestion.variableSetStructureNote, in Displayr we usevariableSetStructure instead of Q's questionType property.
Calculating temporary tables
There are times when you may wish to pull values from a different table with the same dimensions or a modified version of your table that has been filtered or weighted differently. The way this works in Displayr is that you need to calculate a temporary table in order to access these values, as a Rule can’t reference another table in your document. Note that this approach can lead to slow-running tables, especially when used on a large table, a large data set, or both, so care is needed.
Displayr allows you to do this using the calculateTable function, which references table variable sets by variable set label, and filter and weight variables by name (found under Object Inspector > General > Label and Name, respectively):
calculateTable(ROWS, COLUMNS, [filters], weight)To create a table using the same original input variables, filters, and weights, you would do the following:
var tab = calculateTable(table.blue, table.brown, ['!UseQFilters'], '!UseQWeight');To create a table using the same original input variables but with a different filter applied (called target_segment) and a different weight variable applied (called weighting):
var tab = calculateTable(table.blue, table.brown, ["target_segment"], 'weighting');To create a table using different input variables and no filters or weight:
var tab = calculateTable("Q1b - Awareness", "Q3 - Preferred cola", null, null);Note, if your current table has no filter or weight applied, using ['!UseQFilters'] and '!UseQWeight' in your last arguments will respect this.
As with obtaining the statistics from the current table, here we could likewise pull the % statistic from our temporary table called tab, like so:
var vals = tab.get("%");Next
How to Add Table Spans Using a Rule
UPCOMING WEBINAR: The Roadmap for Market Researchers in the Age of AI