Upgrade - Upgrade Brand Health Table

From Q
Jump to navigation Jump to search

The purpose of this QScript is to update any Brand Health Table outputs in your Q project to ensure that they function correctly in the upcoming version of R (R 4.3). If you don't use the Brand Health Table, which is created using Create > Marketing > Brand Health Table, then you do not need to use this QScript.

Technical Details

In the Brand Health Table, if you sort the columns of the table using FORMAT > Sort By > Row 1 (or Row 2 etc) the mechanism that is used to perform the sort is no longer supported in the upcoming version of R. This QScript updates the underlying R code so that the table may still be sorted as desired. The output will be identical after performing this upgrade.

Before Q's R server is upgraded to R4.3, affected brand health tables will show this warning:

BH WARNING MSG.PNG

After the upgrade, the output will instead show an error. Brand Health Tables which were created after the 2nd of May, 2023, will not be affected.

If you have any questions or encounter any issues using this QScript then please contact support@q-researchsoftware.com for assistance.

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.

Customizing QScripts in Q4.11 and more recent versions

  • 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

  • Copy the JavaScript shown on this page.
  • 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 Selection Functions");

let items = [];
recursiveGetAllItemsInGroup(project.report, items);
items = items.filter(function(x) { return x.type == "R Output";});

let update_results = items.map(updateIfBrandHealthTable).filter(x => x.is_brand_health);

if (update_results.length == 0) {
    log("Could not find any Brand Health Tables that needed to be updated. " +
        "Brand Health Tables created after the 1st of May, 2023 " +
        "will already be up to date.\r\n" +
        "If this seems incorrect then please contact support@q-researchsoftware.com");
} else {
    let items_in_error = update_results.filter(x => x.error);
    if (items_in_error.length > 0) {
        log("Some Brand Health Tables were in error after updating. " +
            "Please check that the inputs to these tables are correct, " +
            "and contact support@q-researchsoftware.com if you need assistance.");
        log("Items in error are:");
        log(items_in_error.map(x => x.item.name + ": " + x.msg).join("\r\n"));
    } else {
        log("All Brand Health Tables were updated successfully:");
        log(update_results.map(x => x.item.name).join("\r\n"));
    }    
}




function updateIfBrandHealthTable(item) {
    let output = { item: item, is_brand_health: false, error: false, msg: "" };
    let existing_snippet = `i <- as.numeric(strsplit(sort.by, " ")[[1]][2])
        o <- order(x[i, ], decreasing = TRUE)`;

    let new_string = `x.as.matrix <- as.matrix(x)
        i <- as.numeric(strsplit(sort.by, " ")[[1]][2])
        o <- order(x.as.matrix[i, ], decreasing = TRUE)`;

       let gui_check_string = 'var heading_text = "Brand Health Table";' 

    let r_code = item.code;
    if (r_code.indexOf(existing_snippet) > -1) {
        output.is_brand_health = true;
        r_code = r_code.replace(existing_snippet, new_string);
        item.code = r_code;
        item.update();
        let err = item.error;
        if (err !== null) {
            output.error = true;
            output.msg = err;
        }
    }
    return output;
}