# Filter - Filters from Selected Data

Create a new filter for each category of the selected variable set(s)

This QScript creates a new filter for each category of the selected questions. These filters can then be applied to any tables and charts in the report.

## Technical details

If you have selected a table on the Outputs tab or a variable or question on the Variables and Questions tab, then the function will run on the pre-selected table/question (assuming they're of a permissible type - see below). If you have nothing selected, then you will be presented with a list of the questions in the study, and you should select those that you wish to have filters created from. This list is restricted to questions of the type Pick One, Pick One - Multi, Pick Any, Pick Any - Grid, and Pick Any - Compact. The filters for a question are combined into to a new question, and a table is added to your report to show each of these new questions.

For Pick Any - Grid questions, a copy of the question is made and each variable is set as a filter. For all other question types this QScript will identify changes that have been made in the tables (e.g. merging and renaming of categories) and the filters that are created will reflect those changes.

## 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.

• 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

• 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 Questionnaire Functions');
includeWeb('QScript Value Attributes Functions');
includeWeb('QScript Utility Functions');
includeWeb('QScript Selection Functions');
includeWeb('QScript Functions to Generate Outputs');
includeWeb('QScript Data Reduction Functions');
includeWeb('QScript Functions for Combining Categories');
includeWeb('QScript Functions for Filters');

createFiltersFromSelectedData()

function createFiltersFromSelectedData() {
const web_mode = inDisplayr();
const allowed_types = ["Nominal", "Ordinal", "Nominal - Multi", "Ordinal - Multi", "Binary - Multi",
"Binary - Multi (Compact)", "Binary - Grid"];
let selected_questions = selectInputQuestions(allowed_types);
if (!selected_questions)
return false;
if (!areQuestionsValidAndNonEmpty(selected_questions))
return false;

let filter_questions = selected_questions.map(createFiltersForAllCategories);
filter_questions = filter_questions.filter(function (q) { return q != null; });
if (filter_questions.length == 0) {
log("The selected questions contain no data, or are not appropriate.");
return false;
}
moveQuestionsToHoverButtonIfShown(filter_questions);
reportNewRQuestion(filter_questions, "Filters");
return true;
}


Prior to the 15th of December, 2015, this page was known as Create New Variables - Create Filters from Selected Data