Text Analysis - Advanced - Save Variable(s) - Tidied Text

From Q
Jump to navigation Jump to search

Save a variable to the data set that contains the tidied text

Save the tidied text which has been processed by Text Analysis - Advanced - Setup Text AnalysisInsert > Text Analysis > Advanced > Setup Text Analysis as a new variable in the data set. This blog post describes saving tidied text in order to create a word cloud.


To use this item, first select an item created by Text Analysis - Advanced - Setup Text AnalysisInsert > Text Analysis > Advanced > Setup Text Analysis.


includeWeb("QScript Utility Functions");
includeWeb("QScript Functions to Generate Outputs");
includeWeb("QScript R Output Functions");
includeWeb("QScript Selection Functions");
if (fileFormatVersion() < 9.11)
    log("This script requires a newer version.")
function main() {
    var bad_selection_message = "Select an item that has been created using Text Analysis > Setup Text Analysis.";
    var web_mode = (!!Q.isOnTheWeb && Q.isOnTheWeb());
    var selected_item = getSelectedROutputFromPage(["wordBag"]);
    if (selected_item === null) {
        return false;

    var source_question = selected_item.getInput("formtextvar");
    var target_data_file = source_question.dataFile;
    var r_expression = "library(flipTextAnalysis)\r\n"
                     + "tidied.text = SaveTidiedText(" + stringToRName(selected_item.referenceName) + ")";
    var new_q_name = preventDuplicateQuestionName(target_data_file, "Tidied Text from " + selected_item.name);
    var new_var_name = cleanVariableName(selected_item.name) + "_tidied_text";
    new_var_name = preventDuplicateVariableName(target_data_file, new_var_name);
    try {
        var new_var = target_data_file.newRVariable(r_expression, new_var_name, new_q_name, null);
    } catch (e) {
        log("Save Tidied Text could not be created from this item: " + e);
        return false;
    var new_question = new_var.question;
    // In Q, select the table showing the new variable. 
    if (!web_mode) {
        var new_group = generateGroupOfSummaryTables(new_q_name, [new_question]);
    return true;