This article describes how to use a QScript to add extra information about variables into labels so that it can be seen on tables. The modifications include:
- Adding the variable to the label of each variable.
- Adding the current values of categories to the category labels shown on tables.
- Adding information from the variable names to the corresponding question name.
In general, this QScript should be run immediately prior to exporting or printing. Once data exporting/printing has been completed, the variable names and labels should then be removed using How to Remove Variable Names And Values From Labels. Otherwise, standard operations, such as merging categories, will have ugly and difficult-to-fix outcomes.
The tables below show how the Age question is modified by this QScript. Each of the categories in the rows of the table now features the value or values to which the category currently corresponds in the value attributes. That is, the category 15 and under corresponds to a value of 1, the category 16-24 yrs is formed by merging categories 16-19 yrs and 20-24 yrs, which have values of 2 and 3 respectively, and so on.
The variable names (i.e. q1, q2, etc.) have also been added to the variable labels in the Data Sets tree:
The next table shows how the Unaided Awareness question is modified by this QScript. The variable names have been modified and the row labels now begin with the name of the variable that corresponds to each row.
Requirements
Please note these steps require a Displayr license.
A Displayr Enterprise user license which includes the ability to add custom analysis menu items and QScripts. See How to Use QScripts in Displayr for more details.
Method
To run the QScript to hide variable sets selected from a list:
- From the toolbar menu, select Anything > [Your account name] > Open QScript Editor.
- Paste the following code into the QScript editor:
// Modifying Labels - Add Variable Names And Values To Labels
includeWeb("QScript Functions to Generate Outputs"); if (!main()) log("QScript Cancelled."); else conditionallyEmptyLog("QScript Finished."); function main() { includeWeb("QScript Utility Functions"); var hidden_char = "\u200b "; project.dataFiles.forEach(function (data_file) { data_file.questions.forEach(function (question) { if (!question.isBanner && question.isValid) addVariableNamesAndValuesToLabels(question, hidden_char); }) }); return true; } // This function modifies the question to: // 1. Add the variable name to the question name. When the question has // more than one variable, the common prefix of the variable names // is used. // 2. Add values to the category labels in the data reduction (Pick One // and Pick One - Multi questions only). // 3. Add variable names to the variable labels and corresponding category // labels in the data reduction (Pick One - Multi, Pick Any, and // Number - Multi questions only). // // Label modifications are separated from the original labels using a // hidden character to make it easy to remove the modification using // another QScript function addVariableNamesAndValuesToLabels(question, hidden_char) { var variables = question.variables; var data_reduction = question.dataReduction; var question_type = question.questionType; // Add Variable Name to Question Name if (variables.length == 1) question.name = variables[0].name + hidden_char + question.name; else { var prefix = longestCommonPrefix(variables.map(function (v) { return v.name; })); // Remove non-alphanumeric characters at the end of the prefix while (prefix.length > 0 && prefix.substring(prefix.length - 1, prefix.length).search(/[A-Za-z0-9]/) == -1) prefix = prefix.substring(0, prefix.length - 1); if (prefix.length > 0) question.name = prefix + hidden_char + question.name; } // Add Values to Category Labels (Pick One, Pick One - Multi) if (question_type.indexOf("Pick One") == 0) { var value_attributes = question.valueAttributes; var by_columns = (question_type == "Pick One - Multi" && data_reduction.transposed); var codes = by_columns ? data_reduction.columnLabels : data_reduction.rowLabels; if (codes != null) { // Don't add for main table NET/SUM var net_indices; if (fileFormatVersion() > 8.65) net_indices = by_columns ? data_reduction.netColumns : data_reduction.netRows; else { net_indices = []; codes.forEach(function (code, ind) { if (code == "NET" || code == "SUM") net_indices.push(ind); }); } // Rename codes codes.forEach(function (code, ind) { if (net_indices.indexOf(ind) == -1) { var vals = data_reduction.getUnderlyingValues(code) .map(function (x) { return value_attributes.getValue(x); }) .sort(function (a, b) { return a - b; }); data_reduction.rename(code, vals.join(",") + hidden_char + code); } }); } } // Add Variable Names to Variable Labels (and corresponding data reduction labels) variables.forEach(function (v) { v.label = v.name + hidden_char + v.label; }); var mr_types = ["Pick One - Multi", "Number - Multi", "Pick Any"] if (mr_types.indexOf(question_type) > -1) { var by_columns = (question_type == "Pick One - Multi" && !data_reduction.transposed); var codes = by_columns ? data_reduction.columnLabels : data_reduction.rowLabels; // Don't add for main table NET/SUM var net_indices; if (fileFormatVersion() > 8.65) net_indices = by_columns ? data_reduction.netColumns : data_reduction.netRows; else { net_indices = []; codes.forEach(function (code, ind) { if (code == "NET" || code == "SUM") net_indices.push(ind); }); } codes.forEach(function (code, ind) { if (net_indices.indexOf(ind) == -1) { var vnames = data_reduction.getUnderlyingVariables(code).map(function (v) { return v.name; }); data_reduction.rename(code, vnames.join(",") + hidden_char + code); } }); } } - Click the Play button in the QScript Editor toolbar to run the QScript:
- If you want to save the QScript for future use, click OK then enter a file name for the QScript and click OK. This will save the QScript to your Displayr cloud drive and will then be available in all of your documents by selecting Anything > [Your account name] > Open QScript from Displayr Cloud Drive > [QScript Name]. If you don't wish to save the QScript to the cloud drive, click Cancel in the QScript Editor.
Next
How to Remove Variable Names And Values from Labels
How to Hide a Category in a Pick One Variable Set
How to Hide/Unhide Questions with Many Categories
How to Unhide All Hidden Questions