Modify Whole Table or Plot - Show Only Top K and Specified Rows

From Q
Jump to navigation Jump to search


This rule allows you to select the number of rows to show on a table or chart. The user is able to specify the number of rows (i.e., k), and also to specify particular rows that must always appear. Colors can be assigned to the rows that must always appear. If used after first applying Sorting and Reordering - Sort Rows (Automatically Updates when Data Changes), it allows you to show, for example, the top 3 brands automatically.

Example

This example applies two rules. First, Sorting and Reordering - Sort Rows (Automatically Updates when Data Changes), and then this rule. It has been set so that it shows the top two brands and, if Pepsi appears, it highlights it in green. In this instance, Pepsi was not in the top two rows, so it is appended to the table as a third row.

TopKAndSpecified.png

How to apply this rule

For the first time in a project

  • Select the table(s)/chart(s) that you wish to apply the rule to.
  • Start typing the name of the Rule into the Search features and data box in the top right of the Q window.
  • Click on the Rule when it appears in the QScripts and Rules section of the search results.

OR

  • Select Automate > Browse Online Library.
  • Choose this rule from the list.

Additional applications of the rule

  • Select a table or chart that has the rule and any table(s)/chart(s) that you wish to apply the rule to.
  • Click on the Rules tab (bottom-left of the table/chart).
  • Select the rule that you wish to apply.
  • Click on the Apply drop-down and choose your desired option.
  • Check New items to have it automatically applied to new items that you create. Use Edit > Project Options > Save as Template to create a new project template that automatically uses this rule.

Removing the rule

  • Select the table(s)/chart(s) that you wish to remove the rule from.
  • Press the Rules tab (bottom-right corner).
  • Press Apply next to the rule you wish to remove and choose the appropriate option.

How to modify the rule

  • Click on the Rules tab (bottom-left of the table/chart).
  • Select the rule that you wish to modify.
  • Click Edit Rule and make the desired changes. Alternatively, you can use the JavaScript below to make your own rule (see Customizing Rules).

JavaScript

You can find a detailed discussion of this code at Creating a Custom Rule.

includeWeb("Table JavaScript Utility Functions");

excludeRTables();

// Heading for the form
form.setHeading('Show Only the Top Rows and Specified Rows');
let description = form.newLabel("Show only the top rows in the table, and any specific rows in addition.")
description.lineBreakAfter = true;
 
// Creating input for user to specify number of rows
let number_label = form.newLabel('Number of rows:'); //creating the phrase 'Number of rows:' 
let numeric_up_down = form.newNumericUpDown('threshold'); //creating a control where a user can enter a number.
numeric_up_down.lineBreakAfter = true;
numeric_up_down.setDefault(5);// specifying the default number of rows to be shown
numeric_up_down.setIncrement(1); // specifying that the user must enter whole numbers.
numeric_up_down.setMinimum(0); // preventing the user from having tables with less than 0 rows (not 1, as a user may wish to only specify the brands)
let control_array = [description, number_label, numeric_up_down];
 
// Textboxes for selecting rows to always retain
let label_row = form.newLabel('Rows to always retain:');
label_row.lineBreakAfter = true;
let text_box = form.newTextBox('textBox');
text_box.lineBreakAfter = true;
control_array.push(label_row);
control_array.push(text_box);
form.setInputControls(control_array);
 
// Adding addition text boxes, if required
let last_selection = text_box.getValue();
let required_row_labels = [];
while (last_selection != "") {
    required_row_labels.push(last_selection);
    let new_name = "textBox" + (control_array.length - 1);
    let new_menu = form.newTextBox(new_name);
    last_selection = new_menu.getValue();
    control_array.push(new_menu);
    form.setInputControls(control_array);
    new_menu.lineBreakAfter = true;
}


// control for the output color
let label_color = form.newLabel('Color for required rows:');
let color_picker = form.newColorPicker('color');
color_picker.setDefault('White');
form.setInputControls(control_array.concat([label_color, color_picker]));
 
// control for the output color
let color = color_picker.getValue();
form.setOutputColor(color);
 
// Extracting the number of rows provided by the user and assigning it to a variable.
let maximum_number_rows = numeric_up_down.getValue();
 
// Creating a summary
let summary = 'Show top ' + maximum_number_rows + ' rows';
if (required_row_labels.length != 0)
    summary += '; always show "' + required_row_labels.join('", "') + '"';
form.setSummary(summary);
 
// Deleting the rows more than the specified number (unless they are required)
let row_labels = table.rowLabels;
let n_extra_required_rows = 0;
while (table.numberRows > maximum_number_rows + n_extra_required_rows) {
    let last_row = table.numberRows - 1 - n_extra_required_rows;
    let last_label = row_labels[last_row];
    if (required_row_labels.indexOf(last_label) != -1) {
        n_extra_required_rows++;
    } else {
        deleteRowComplete(last_row);
    }
}
 
// Coloring rows
row_labels = table.rowLabels; //getting again as may have changed
let colors = table.cellColors;
row_labels.forEach(function (label, index) {
    if (required_row_labels.indexOf(row_labels[index]) != -1) { // check if row label contains text
        for (let i = 0; i < table.numberColumns; i++) // set color for each element in row
            colors[index][i] = color;
    }
});
if (table.numberRows != 0)
    table.cellColors = colors;
 
// Returns true if Statistics - Right are available for this table.
// This function needs to be kept in the main body of the rule to ensure
// that right_table is included properly before the rule code is
// executed.
function rightTableExists() {
    let exists = true;
    try { 
        right_table.statistics;
    } catch (e) {
        exists = false;
    }
    return exists;
}

See also