Tables - Banner Tables

From Q
Jump to navigation Jump to search

Create a new banner and run crosstabs with that banner

Related Videos
Part 4 of Creating Tables in Q (Video)
 

This QScript generates banner tables for a list of specified questions. This is an alternative to creating a Banner and then using Basic Tables to generate crosstabs with the banner, and automatically flattens any two-dimensional (grid) questions.

Example

BannerExample.png

Technical details

You will be asked to specify:

  1. The questions to place in the rows of the tables.
  2. The questions to use in the banner.

Any Pick One - Multi, Pick Any - Grid and Number - Grid questions will be copied and flattened before using them in a crosstab. The flattened copy will contain all of the cells from the original question in a single column or row.

How to apply this QScript

  • Start typing the name of the QScript into the Search features and data box in the top right of the Q window.
  • Click on the QScript when it appears in the QScripts and Rules section of the search results.

OR

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

Customizing the QScript

This QScript is written in JavaScript and can be customized by copying and modifying the JavaScript.

Customizing QScripts in Q4.11 and more recent versions

  • Start typing the name of the QScript into the Search features and data box in the top right of the Q window.
  • Hover your mouse over the QScript when it appears in the QScripts and Rules section of the search results.
  • Press Edit a Copy (bottom-left corner of the preview).
  • Modify the JavaScript (see QScripts for more detail on this).
  • Either:
    • Run the QScript, by pressing the blue triangle button.
    • Save the QScript and run it at a later time, using Automate > Run QScript (Macro) from File.

Customizing QScripts in older versions

  • Copy the JavaScript shown on this page.
  • Create a new text file, giving it a file extension of .QScript. See here for more information about how to do this.
  • Modify the JavaScript (see QScripts for more detail on this).
  • Run the file using Automate > Run QScript (Macro) from File.

JavaScript

includeWeb('QScript Selection Functions');
includeWeb('QScript Table Functions');
includeWeb('QScript Functions to Generate Outputs');
includeWeb('QScript Utility Functions');

createBannerTables()

function createBannerTables() {

    const web_mode = inDisplayr();

    const allowed_column_types = web_mode ? ["Pick One", "Pick Any", "Pick Any - Compact"]
                                          : ["Pick One", "Pick Any", "Pick Any - Compact", "Pick One - Multi", "Pick Any - Grid"];

    let column_q_choices = [];
    let row_q_choices = [];
    
    let data_file = requestOneDataFileFromProject(false);
     
    // Work out all questions which may be presented to the user 
    let candidate_questions = data_file.questions;
    candidate_questions = candidate_questions.filter(function (q) {
        return !q.isHidden && q.questionType.indexOf("Text") == -1 && q.isValid;
    });
    // Exclude any questions where all the categories are set to missing
    candidate_questions = candidate_questions.filter(function (q) { 
                                                        if (q.questionType.indexOf("Pick One") == 0) {
                                                            var uniques = q.uniqueValues;
                                                            var value_attributes = q.valueAttributes;
                                                            var non_missings = uniques.filter(function (x) { return !value_attributes.getIsMissingData(x); });
                                                            return non_missings.length > 0;
                                                        } else
                                                            return true;
                                                    });
 
    candidate_questions.forEach(function (q) {
        row_q_choices.push(q);
        if (allowed_column_types.indexOf(q.questionType) > -1 && !q.isBanner)
            column_q_choices.push(q);
    });
 
    if (row_q_choices.length == 0 || column_q_choices.length == 0) {
        log(correctTerminology("There are no appropriate questions to use for banner tables."));
        return false;
    }

    let row_questions;
    let column_questions;
    while (!row_questions || !oneOrMoreQuestions(row_questions))
        row_questions = selectManyQuestions(correctTerminology('Please select questions to place in the rows:'), row_q_choices).questions;
 
    while (!column_questions || !oneOrMoreQuestions(column_questions))
        column_questions = selectManyQuestions(correctTerminology('Please select questions to place in the banner:'), column_q_choices).questions;
    
    flattenSelectedQuestions(column_questions); 
    flattenSelectedQuestions(row_questions);
 
    // place each column question inside an array
    column_questions = column_questions.map(function (q) { return [q]; });
 
    // create banner from column questions
    let banner_name = preventDuplicateQuestionName(data_file, 'BANNER');
 
    let banner_q;
    if (fileFormatVersion() >= 8.58) {
        banner_q = data_file.createBanner(banner_name, column_questions, false, true);
    } else {
        banner_q = data_file.createBanner(banner_name, column_questions);
 
        // remove NET from each span
        for (var i = 0; i < banner_q.variables.length; i++) {
            let variable = banner_q.variables[i];
            for (var j = 0; j < column_questions.length; j++)
                if (variable.label == column_questions[j][0].name + ': NET') {
                    variable.deleteVariable();
                    break;
                }
        }
    }
      
    if (!largeCrosstabsAllowed(row_questions, [banner_q])) 
        return false;
    else {
        
        let output_pairs = row_questions.map(function (q) {
            return {primary: q, secondary: banner_q};
        });

        let page_names = row_questions.map(function (q) { return q.name; });

        let statistics = [];

        let report = createReport("Banner Tables",
                          output_pairs,
                          page_names,
                          statistics,
                          null,
                          [],
                          false,
                          1,
                          false,
                          false);

        if (!web_mode) {
            let new_tables = [];
            recursiveGetAllTablesInGroup(report, new_tables);
            new_tables.forEach(function (table) {
                if (!addStat(table, 'Row %') * !addStat(table, 'n')) // non short-circuiting &&
                    addStat(table, 'Column n');
            })
        }

        let report_pages = report.subItems.filter(x=> x.type != "Text");
        if (fileFormatVersion() > 8.65)
            project.report.setSelectedRaw([report_pages[0]]);
        
        return true;
    }
}
 

function addStat(table, stat) {
    cell_stats = table.cellStatistics;
    cell_stats.push(stat);
    table.cellStatistics = cell_stats;
    return table.cellStatistics.indexOf(stat) > -1;
}

See also