MicroStrategy ONE
The R Integration Pack is no longer supported as of December 2024.
Configuring Dual Execution Modes
When developing analytics in R, a common practice is to use an iterative process to ensure that it works as expected. That process usually involves the R script taking inputs that are either created by the script or read from a data source, such as a file or a database. The results of the script are usually returned to the R console. But when the script is deployed to MicroStrategy, MicroStrategy provides the inputs to the R script and uses the R script outputs.
While it is possible to have the same analytics implemented in two different scripts, one for running from the console and one for execution by MicroStrategy, this approach requires maintaining two scripts. In this scenario, it can be difficult to keep the scripts synchronized as changes occur.
MicroStrategy provides the execution flag mstr.ExFlag, which exists only when MicroStrategy executes the R script. This means that you can use the existence of the flag to determine if MicroStrategy executed the R script. You can develop your script to use this flag and react to whether the script is run from the R console or executed by MicroStrategy.
The example shown below includes code inserted to adapt the R Integration Pack forecasting script example to generate its own data when it is not executed by MicroStrategy. The addition is referenced in mstr.ExFlag in the code below.
# Modeling
# 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])
# Get data
# If this is executed by MicroStrategy
if (exists("mstr.ExFlag")) {
# Create a data frame from the input variables
df <- data.frame(cbind(Target, Trend, Season))
# If InputNames is non-empty
if (length(mstr.InputNames) > 0) {
# Name these variables
colnames(df) <- mstr.InputNames }
# If this is NOT via a MicroStrategy Report Execution
} else {
# Set random number seed for consistency
set.seed(42)
# Set Trend variable for 48 months
Trend <- seq(1:48)
# Set Season variable for 4 years of 12 months
Season <- rep(seq(1:12),4)
# Set 3 years of linear but noisy values for the Target
Target <- (seq(1:36)*(0.8+(0.4*runif(36,0,1))))
# Add the forecast horizon Target <append(Target, c(rep(NA, 12)))
# Create a data frame from the input variables
df <data.frame(cbind(Target, Trend, Season))
# Set the name for saving output
FileName <"SeasonalForecasting_console"
}
# Modeling
# 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])
Creating a Data Frame
Since most analytics operate on a table of data, known as a data frame in R, it is often helpful to combine input variables in a data frame.
In the seasonal forecast data example, the three input variables (target, trend, and season) are combined into a data frame.
Since both execution flows use the same data frame object, you can compare the data used when MicroStrategy executes the script with the data used when the R console executes the script.
While there are reasons to avoid using a data frame in either flow, such as if the data frame causes undesired side effects like performance problems, having the data in the same object for both flows allows for easier comparison.