Modify Labels - Modify Text in Selected Items

From Q
Jump to navigation Jump to search
This page is currently under construction, or it refers to features which are under development and not yet available for use.
This page is under construction. Its contents are only visible to developers!

This tool allows you to rename the default lables which appear in tables, footers, and other places in Displayr.

Key examples of things you may wish to rename using this tool are:

  1. Table statistics. For example, you may wish to rename Column Sample Size as BASE.
  2. Labels for quarters in Date variables. For example, the default labels which appear when quarterly date aggregation is used are Jan-Mar, Apr-Jun, Jul-Sep, Oct-Dec, but you may simply wish to rename these to Q1, Q2, Q3, and Q4. If you do so, the year will still appear in the label.
  3. Other default labels, like NET or SUM, which you may want to display as Total.

You can choose to rename the desired labels for the entire document as new defaults, or you can choose to change them for a single selected table.

Usage

  1. Select one or more tables where you wish to apply the renaming.
  2. Select Anything > Table > Modify Text in Selection.
  3. If you wish to set the defaults for the entire document, select Yes when prompted. If you only wish to modify the selected tables, then click No.
  4. Choose one or more items from the list of statistics. If the list displays any statistic names in brackets, [...], then this indicates that the label has been changed already for at least one of the tables you have selected.
  5. Type in a new name for each statistic you have selected. You will be prompted separately for each.

Renaming Quarters

To rename the labels displayed for quarters, select the following four items in the list when prompted to choose which labels to rename:

  • (quarter Jan..Mar)
  • (quarter Apr..Jun)
  • (quarter Jul..Sep)
  • (quarter Oct..Dec)

Technical limitations

There are currently some technical limitations with the renaming of quarter labels in Displayr:

  1. Quarter labels may only be renamed for the document defaults and it is not possible to rename them for individual tables.
  2. The modified quarter labels will not appear in tables or visualizations until your document has been reloaded. This will happen if you close your browser and return after your document has gone to sleep. There are two main methods to force the document to update more quickly:
    1. Select the name of your data set under Data Sets, select Properties > GENERAL > Update on the right, and then update your data set using the same file or source which is currently in use.
    2. Alternatively, you can select Publish > Download Document (.QPack) which forces Displayr to save the current version of your work, and then go into the document's Version History section and click the Use this version option for the version at the top of the list (which corresponds to the one which you just saved).


includeWeb("QScript Utility Functions");
includeWeb("QScript Selection Functions");
includeWeb("JavaScript Array Functions");


setTranslations();

function setTranslations() {
    const user_selections = getAllUserSelections();

    const menu_items = [{q_name:'(quarter Jan..Mar)', displayr_name:'(quarter Jan..Mar)'},
                        {q_name:'(quarter Apr..Jun)', displayr_name:'(quarter Apr..Jun)'},
                        {q_name:'(quarter Jul..Sep)', displayr_name:'(quarter Jul..Sep)'},
                        {q_name:'(quarter Oct..Dec)', displayr_name:'(quarter Oct..Dec)'},
                        {q_name:'NET', displayr_name:'NET'},
                        {q_name:'SUM', displayr_name:'SUM'},
                        {q_name:'%', displayr_name:'%'},
                        {q_name:'% Column Responses', displayr_name:'% Column Responses'},
                        {q_name:'% Column Share', displayr_name:'% Column Share'},
                        {q_name:'% Excluding NaN', displayr_name:'% Excluding NaN'},
                        {q_name:'% of total', displayr_name:'% of total'},
                        {q_name:'% Responses', displayr_name:'% Responses'},
                        {q_name:'% Row Responses', displayr_name:'% Row Responses'},
                        {q_name:'% Row Share', displayr_name:'% Row Share'},
                        {q_name:'% Share', displayr_name:'% Share'},
                        {q_name:'% Total Responses', displayr_name:'% Total Responses'},
                        {q_name:'% Total Share', displayr_name:'% Total Share'},
                        {q_name:'% within column', displayr_name:'% within column'},
                        {q_name:'% within row', displayr_name:'% within row'},
                        {q_name:'25th Percentile', displayr_name:'25th Percentile'},
                        {q_name:'5th Percentile', displayr_name:'5th Percentile'},
                        {q_name:'75th Percentile', displayr_name:'75th Percentile'},
                        {q_name:'95th Percentile', displayr_name:'95th Percentile'},
                        {q_name:'AND', displayr_name:'AND'},
                        {q_name:'Average', displayr_name:'Average'},
                        {q_name:'bars', displayr_name:'bars'},
                        {q_name:'Base n', displayr_name:'Sample Size'},
                        {q_name:'Base Population', displayr_name:'Weighted Sample Size'},
                        {q_name:'Base WW', displayr_name:'Base WW'},
                        {q_name:'Biweekly (2 weeks)', displayr_name:'Biweekly (2 weeks)'},
                        {q_name:'Bonferroni', displayr_name:'Bonferroni'},
                        {q_name:'Bonferroni (pooled t-test)', displayr_name:'Bonferroni (pooled t-test)'},
                        {q_name:'Coefficient', displayr_name:'Coefficient'},
                        {q_name:'Column %', displayr_name:'Column %'},
                        {q_name:'Column Comparisons', displayr_name:'Column Comparisons'},
                        {q_name:'Column n', displayr_name:'Column Sample Size'},
                        {q_name:'Column Names', displayr_name:'Column Names'},
                        {q_name:'Column Standard Error', displayr_name:'Column Standard Error'},
                        {q_name:'Column Standard Error of Mean', displayr_name:'Column Standard Error of Mean'},
                        {q_name:'Column WW', displayr_name:'Column WW'},
                        {q_name:'columns', displayr_name:'columns'},
                        {q_name:'Columns Compared', displayr_name:'Columns Compared'},
                        {q_name:'Compare to previous period', displayr_name:'Compare to previous period'},
                        {q_name:'Compare to rest of data', displayr_name:'Compare to rest of data'},
                        {q_name:'Corrected p', displayr_name:'Corrected p'},
                        {q_name:'Correlation', displayr_name:'Correlation'},
                        {q_name:'Cumulative %', displayr_name:'Cumulative %'},
                        {q_name:'d.f.', displayr_name:'d.f.'},
                        {q_name:'Day', displayr_name:'Day'},
                        {q_name:'Duncan', displayr_name:'Duncan'},
                        {q_name:'Dunnett', displayr_name:'Dunnett'},
                        {q_name:'Effective Base n', displayr_name:'Effective Sample Size'},
                        {q_name:'Effective Column n', displayr_name:'Effective Column Sample Size'},
                        {q_name:'Effective n', displayr_name:'Effective Count'},
                        {q_name:'Effective Row n', displayr_name:'Effective Row Sample Size'},
                        {q_name:'Expected %', displayr_name:'Expected %'},
                        {q_name:'Expected Average', displayr_name:'Expected Average'},
                        {q_name:'Expected Correlation', displayr_name:'Expected Correlation'},
                        {q_name:'Expected n', displayr_name:'Expected n'},
                        {q_name:'False Discovery Rate (FDR)', displayr_name:'false discovery rate correction'},
                        {q_name:'Fisher LSD', displayr_name:'Fisher LSD'},
                        {q_name:'Four week', displayr_name:'Four week'},
                        {q_name:'from {0} to {1}', displayr_name:'from {0} to {1}'},
                        {q_name:'Index', displayr_name:'Index'},
                        {q_name:'Interquartile Range', displayr_name:'Interquartile Range'},
                        {q_name:'Labels', displayr_name:'Labels'},
                        {q_name:'Lower Confidence Interval', displayr_name:'Lower Confidence Interval'},
                        {q_name:'Lower Confidence Interval %', displayr_name:'Lower Confidence Interval %'},
                        {q_name:'Maximum', displayr_name:'Maximum'},
                        {q_name:'Median', displayr_name:'Median'},
                        {q_name:'Minimum', displayr_name:'Minimum'},
                        {q_name:'Missing n', displayr_name:'Missing Count'},
                        {q_name:'Mode', displayr_name:'Mode'},
                        {q_name:'Month', displayr_name:'Month'},
                        {q_name:'moving averages', displayr_name:'moving averages'},
                        {q_name:'Multiple Comparison Adjustment', displayr_name:'Multiple Comparison Adjustment'},
                        {q_name:'n', displayr_name:'Count'},
                        {q_name:'n Observations', displayr_name:'n Observations'},
                        {q_name:'Newman Keuls (S-N-K)', displayr_name:'Newman Keuls (S-N-K)'},
                        {q_name:'None', displayr_name:'no correction'},
                        {q_name:'None (pooled t-test)', displayr_name:'None (pooled t-test)'},
                        {q_name:'NOT', displayr_name:'NOT'},
                        {q_name:'Not Duplicate', displayr_name:'Not Duplicate'},
                        {q_name:'Not significant', displayr_name:'Not significant'},
                        {q_name:'Observation', displayr_name:'Observation'},
                        {q_name:'OR', displayr_name:'OR'},
                        {q_name:'p', displayr_name:'p'},
                        {q_name:'Page {0}', displayr_name:'Page {0}'},
                        {q_name:'Pop.', displayr_name:'Pop.'},
                        {q_name:'Probability %', displayr_name:'Probability %'},
                        {q_name:'Quarter', displayr_name:'Quarter'},
                        {q_name:'Range', displayr_name:'Range'},
                        {q_name:'Residual', displayr_name:'Residual'},
                        {q_name:'Residual %', displayr_name:'Residual %'},
                        {q_name:'Row %', displayr_name:'Row %'},
                        {q_name:'Row n', displayr_name:'Row Sample Size'},
                        {q_name:'Row WW', displayr_name:'Row WW'},
                        {q_name:'Significantly down', displayr_name:'Significantly down'},
                        {q_name:'Significantly up', displayr_name:'Significantly up'},
                        {q_name:'Standard Deviation', displayr_name:'Standard Deviation'},
                        {q_name:'Standard Error', displayr_name:'Standard Error'},
                        {q_name:'Sum', displayr_name:'Sum'},
                        {q_name:'Table of Contents', displayr_name:'Table of Contents'},
                        {q_name:'Text With No Blanks', displayr_name:'Text With No Blanks'},
                        {q_name:'Total %', displayr_name:'Total %'},
                        {q_name:'trend lines: {0}..{1}', displayr_name:'trend lines: {0}..{1}'},
                        {q_name:'Trimmed Average', displayr_name:'Trimmed Average'},
                        {q_name:'t-Statistic', displayr_name:'t-Statistic'},
                        {q_name:'Tukey HSD', displayr_name:'Tukey HSD'},
                        {q_name:'Unique Text', displayr_name:'Unique Text'},
                        {q_name:'Upper Confidence Interval', displayr_name:'Upper Confidence Interval'},
                        {q_name:'Upper Confidence Interval %', displayr_name:'Upper Confidence Interval %'},
                        {q_name:'Values', displayr_name:'Values'},
                        {q_name:'values are {0}', displayr_name:'values are {0}'},
                        {q_name:'Week', displayr_name:'Week'},
                        {q_name:'Column Population', displayr_name:'Weighted Column Sample Size'},
                        {q_name:'Population', displayr_name:'Weighted Count'},
                        {q_name:'Row Population', displayr_name:'Weighted Row Sample Size'},
                        {q_name:'WW', displayr_name:'WW'},
                        {q_name:'Year', displayr_name:'Year'},
                        {q_name:'z-Statistic', displayr_name:'z-Statistic'},
                        {q_name:'{0} commencing', displayr_name:'{0} commencing'},
                        {q_name:'{0} x{1} commencing', displayr_name:'{0} x{1} commencing'},
                        {q_name:'{0}: {1}..{2}', displayr_name:'{0}: {1}..{2}'}]

    // Determine whether the user wishes to change the current item or if they want to set the default for their
    // document.
    let project_level = true;
    if (NOrMoreSelected(user_selections, "Table", 1, false) || NOrMoreSelected(user_selections, "Plot", 1, false)) {
        project_level = askYesNo("Do you want to set the default for this document? If you click No, only the selected output(s) will change.");
    }

    // Collect all selected tables and plots
    let relevant_selections = [].concat(user_selections.implicitly_selected_tables)
                                .concat(user_selections.implicitly_selected_plots);

    

    let translations_info = menu_items.map(function (obj) {
        let term = obj.q_name;
        let displayr_term = obj.displayr_name;

        // Get the project-level translation, which is the current displayr text
        let project_translation = project.translations.get(term);
        
        // Get all translations for this term from selected items
        let current_translations = project_level ? [project_translation]
                                                 : relevant_selections.map(function (item) { return item.translations.get(term); });

        // Remove duplicated
        current_translations = uniqueElementsInArray(current_translations);

        // Remove default project translation if present
        current_translations = current_translations.filter(function (term2) { return term2 != displayr_term; });

        return {original: term,
            displayr_term: displayr_term, 
            project_translation: project_translation, 
            current_translations: current_translations};
    });

    // Format the text for the user to select in the menu.
    // If there are existing translations on selected items,
    // show them within [ .. ]
    let text_to_show = translations_info.map(function (obj) { 
        return obj.displayr_term + (obj.current_translations.length > 0 ? " [" + obj.current_translations.join(", ") + "]" : "");
    });

    let translations_to_set = [];
    let continue_prompt = true;

    // User selects which term to translate
    let selected_inds = selectMany("Select one or more labels to modify:", text_to_show);
    let selected_terms = getElementsOfArrayBySelectedIndices(translations_info, selected_inds);

    // User enters new name for term
    let new_name;
    selected_terms.forEach(function (selected_term) {
        new_name = "";
        while (new_name.length == 0) {
            new_name = prompt("Enter a new label for " 
                + selected_term.project_translation 
                + (selected_term.current_translations.length > 0 ? " (currently translated to " + selected_term.current_translations.join(", ") + "):" : ":" ));
            if (new_name.length == 0)
                log("Blank labels are not allowed.");
        }
        translations_to_set.push({original: selected_term.original, new: new_name});
    })

    function userHasSelectedQuarters(translations_to_set) {
        let quarter_regex = new RegExp('quarter');
        return translations_to_set.some(obj => quarter_regex.test(obj.original))
    }
    
    if (!project_level && userHasSelectedQuarters(translations_to_set)) {
        let switch_to_project_level = confirm("Quarter names can only be set for your entire document. Click OK to set the new labels as the defaults for this document.");
        if (!switch_to_project_level)
            return;
        project_level = true;
    }
    
    if (project_level) {
        translations_to_set.forEach(function (translation) {
            project.translations.set(translation.original, translation.new);
        });
        // Hack to force translation to show on tables when setting project-level
        let tables_in_report = [];
        recursiveGetAllTablesInGroup(project.report, tables_in_report);
        tables_in_report.forEach(function (x) {
            try {                
                x.changeDecimalPlacesBy(1);
                x.changeDecimalPlacesBy(-1);
                x.calculateOutput();
                if (x.type == "Table")
                    x.cellStatistics
                else 
                    x.mainStatistics;
            } catch (e) {}
        });

    } else {
            // Go through all selected items and set the translation
            relevant_selections.forEach(function (item) {
                translations_to_set.forEach(function (translation) {
                    item.translations.set(translation.original, translation.new);
                });
            });    
    }    

    if (userHasSelectedQuarters(translations_to_set)) {
        log("The new quarter names will not appear in your tables and visualizations until your document reloads.\r\n"
            + "The quickest way to do reload is:\r\n"
            + "1. Select the name of your data set under 'Data Sets'.\r\n"
            + "2. Click 'Properties > GENERAL > Update' on the right of the screen.\r\n"
            + "3. Update your data set using the same file or source that is currently being used.");
    }


    return;

}

See also