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

From Q
Jump to navigation Jump to search

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.

Usage

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

Code

includeWeb("QScript Utility Functions");
includeWeb("QScript Functions to Generate Outputs");
includeWeb("QScript R Output Functions");
includeWeb("QScript Selection Functions");

saveTidiedText();
 
function saveTidiedText() {
    let web_mode = inDisplayr();
 
    let selected_item = getSelectedROutputFromPage(["wordBag"]);
    if (selected_item === null) {
        log("Select an item that has been created using Text Analysis > Setup Text Analysis.");
        return false;
    }
    let target_data_file = getDataFileFromItemDependants(selected_item);
    if (target_data_file == null) {
        log("'Save variables' cannot be applied to an output with no associated data file.");
        return;
    }
    let r_expression = 
`library(flipTextAnalysis)
tidied.text = SaveTidiedText(${stringToRName(selected_item.referenceName)})`;
 
    let new_q_name = preventDuplicateQuestionName(target_data_file, "Tidied Text from " + selected_item.name);
    let new_var_name = cleanVariableName(selected_item.name) + "_tidied_text";
    new_var_name = preventDuplicateVariableName(target_data_file, new_var_name);
    let new_var;
    try {
        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;
    }
    let new_question = new_var.question;
 
 
    // In Q, select the table showing the new variable. 
    if (!web_mode) {
        let new_group = generateGroupOfSummaryTables(new_q_name, [new_question]);
        project.report.setSelectedRaw([new_group.subItems[0]]);
    }
 
    return true;
}