MicroStrategy ONE

The R Integration Pack is no longer supported as of December 2024.

Implementing Error Handling

Errors or exceptions encountered while executing code enclosed within a tryCatch function are caught and returned to MicroStrategy in the R variable called mstr.ErrMsg (this variable is a reserved name for this purpose).

If an error is returned, MicroStrategy creates an error log file with the message. If your script generates any output files, this error log file should appear in the same location. It is good practice to use a working directory specific to your script to keep its error log files separate from those from other scripts.

The error log file is called RScriptErrors.log and users can find it in the supporting directory for the R script.

  • If the R analytics is deployed as a stand-alone metric, the error causes the report or document execution to fail, resulting in the error message being displayed to the user.
  • Errors in R analytics deployed as derived metrics or stand-alone metrics that are configured as smart metrics typically do not cause report or document execution to fail. A smart metric calculates subtotals on the individual elements of a metric. Instead, null results are displayed as if empty values were returned by the R analytic. Empty results can indicate that there was a problem executing the R analytics and that you should check the log file for more details.

For steps to define stand-alone metrics as smart metrics, also referred to as smart totals, as well as background information on smart metrics, refer to the Advanced Reporting Help. In both cases described above, a message is logged in the DSSErrors.log file, unless logging is disabled using the MicroStrategy Diagnostics Utility.

The DSSErrors.log is typically found at X:\Program Files (x86)\Common Files\MicroStrategy\Log, where X: is the drive where MicroStrategy is installed.

In addition to errors caught by the tryCatch function, other conditions can result in error logging, including:

  • Errors during the processing of registry entries, for example, problems with the keys associated with the InstallPath and RScriptsFolder. These errors do not cause a report to fail.
  • Errors loading the R library or initializing the R environment.
  • Errors parsing the R script header block.
  • Data type mismatches.
  • Failure locating the R script specified by the _RScriptFile parameter for the R script's metric in MicroStrategy. This error can occur if a bad path or file name is specified, or if the script is not found at the location specified by the RScriptsFolder registry key.

The example R script described in R Script Example includes a command to set the working directory. It is recommended that this line of code is moved to the body of the script, within a tryCatch function wrapper. This ensures that any potential failure of the setwd() command is caught.

The following example shows how to wrap the R Integration Pack forecasting example code in a tryCatch function to catch errors:

Copy
# tryCatch for Exception Handling
mstr.ErrMsg <tryCatch({
# Create a data frame from the input variables
df <data.frame(cbind(Target, Trend, Season))
# Train model on all records with Target values
model <- lm(Target ~ Trend + factor(Season),data=df[!is.na(Target), ])
# Return predictions from the model
Forecast <- predict(model, newdata = df[, -1])
# Print success message when run from the console
try(print("Success!"))
# If we made it here, no errors were caught
mstr.ErrMsg <""
# Catch block to report an error
}, error = function(err) {
# Print error message if run from console
try(print(err))
# Return error message
return(err$message)
})