Preliminary Project Setup - Hide Uninteresting Data

From Q
Jump to: navigation, search

This QScript presents to the user with a list of questions/variables in the data file that may be uninteresting and allows the user to hide them so that they do not appear in menus and tables.

Technical details

Uninteresting questions in this script are defined to be those that have:

  • Identical values for all respondents.
  • Different values for each respondent.

The user selects questions that they do not wish to hide, and the remaining questions are hidden by the script. The user is provided with the option to delete the tables and charts that depend on the hidden questions (and are therefore broken).

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('JavaScript Array Functions');
includeWeb('QScript Selection Functions');
includeWeb('QScript Table Functions');
includeWeb('QScript Data Reduction Functions');
includeWeb('QScript Functions to Generate Outputs');

if (!main())
    log("QScript cancelled.");
else
    conditionallyEmptyLog("QScript Finished.");

function main() {
    var data_file = requestOneDataFileFromProject(false, true);
    var suggested_questions = [];
    var hidden_q_names = [];
    var items = [];
    var deleted_tables_names = [];
    var deleted_plots_names = [];
    var is_cancelled = false;

    // Generate an array of uninteresting questions
    var candidate_questions = data_file.questions.filter(function (q) { return !q.isHidden; });
    candidate_questions.forEach(function (q) {
        var variables = q.variables;
        if (variables.length == 1 && variables[0].rawValues.length == variables[0].countUniqueValues)
            suggested_questions.push(q);
        else if (variables.every(areRawValuesIdentical))
            suggested_questions.push(q);
        else if (["Pick One", "Pick One - Multi", "Pick Any", "Pick Any - Grid", "Pick Any - Compact"].indexOf(q.questionType) > -1)
            if (largestDimension(q) > 50)
                suggested_questions.push(q);    
    });

     
     
    if (suggested_questions.length == 0) {
        log('There are no uninteresting questions.');
        return false;
    }
    else {
        var selection_message = 'We think these questions may not be interesting.'
                                + 'Please select any questions that you think are interesting. The remaining questions will be hidden.'
                                + 'You can unhide them at any time on the Variables and Questions tab.';

        var selected_questions = selectManyQuestions(selection_message, suggested_questions, false);
        

        for (var i = 0; i < suggested_questions.length; i++) {
            var suggested_q = suggested_questions[i];
            if (selected_questions.names.indexOf(suggested_q.name) == -1) {
                suggested_q.isHidden = true;
                hidden_q_names.push(suggested_q.name);
            }
        }
 
        if (hidden_q_names.length > 0) {
            log('The following questions have been hidden:');
            for (var i = 0; i < hidden_q_names.length; i++) {
                log(hidden_q_names[i]);
            }
            log('');
 
            var delete_broken = confirm('Hiding questions may cause tables and plots to break. ' +
            'Would you like to delete broken tables and plots?')
 
            if (delete_broken) {
                recursiveGetAllTablesAndPlotsInGroup(project.report, items);
                for (var i = 0; i < items.length; i++) {
                    var item = items[i];
                    var is_broken = false;
                    if (hidden_q_names.indexOf(item.primary.name) != -1) {
                        is_broken = true;
                    }
                    if (item.secondary != null && hidden_q_names.indexOf(item.secondary.name) != -1) {
                        is_broken = true;
                    }
                    if (item.tertiary != null && hidden_q_names.indexOf(item.tertiary.name) != -1) {
                        is_broken = true;
                    }
                    if (is_broken) {
                        if (item.type == 'Table') {
                            deleted_tables_names.push(item.name);
                        }
                        else {
                            deleted_plots_names.push(item.name);
                        }
                        item.deleteItem();
                    }
                }
            }
 
            if (deleted_tables_names.length > 0) {
                log('The following tables have been deleted:');
                for (var i = 0; i < deleted_tables_names.length; i++) {
                    log(deleted_tables_names[i]);
                }
                log('');
            }
 
            if (deleted_plots_names.length > 0) {
                log('The following plots have been deleted:');
                for (var i = 0; i < deleted_plots_names.length; i++) {
                    log(deleted_plots_names[i]);
                }
            }
        }
        else {
            log('No questions have been hidden.');
        }
    }
    return true;
}

function areRawValuesIdentical(variable) {
    var raw = variable.rawValues;
    var uniques = uniqueElementsInArray(raw);
    return uniques.length == 1;
}


See also