R Integration User Guide
R Integration User Guide
Guide
Version: 1.0
Document Number: 09600106
CONTENTS
Book Overview
1. Using the R
Integration Pack
Index........................................................................................................................................... 35
Contents
vi
BOOK OVERVIEW
Description of Guide
This document describes the R Integration Pack, which facilitates the
deployment of analytics from the R statistical environment to MicroStrategy.
It is intended to help MicroStrategy users extend the analytical features of
the MicroStrategy platform using the capabilities of the R platform. The R
Integration Pack requires a general understanding of the MicroStrategy
Business Intelligence environment, such as creating metrics and using them
on reports, as well as a familiarity with the R programming environment.
vii
Book Overview
The sample R script provided with the R Integration Pack has been
updated to include various enhancements. This updated script is
referenced in the various topics related to Best practices: Making the R
script robust, page 13 as well as R analytic deployment example,
page 31.
Version 1.0
viii
This guide was created for the R Integration Pack version 1.0.
Book Overview
Additional formats
MicroStrategy manuals are available as electronic publications,
downloadable on the Apple iBookstore or Google Play, and can be read on
your iOS or Android device respectively. To download a book, search for the
books title in the iBookstore or Google Play respectively. To view a list of
manuals that are currently available, scan the following QR codes using your
devices camera:
new MicroStrategy releases, it may take several days for the latest
For
manuals to be available on the iBookstore or Google Play.
ix
Book Overview
1
1.
Introduction
R is the open-source, industry-leading language and environment for
statistical computing and graphics. The R Integration Pack lets you easily
integrate this statistical computing and graphics environment into
MicroStrategy by deploying R analytics as standard MicroStrategy metrics
that implement R scripts.
The R Integration Pack is part of the MicroStrategy Function Plug-in family
of approaches to adding new analytics to any MicroStrategy environment, as
shown below:
For information on C++ Function Plug-ins, see the MicroStrategy Help for
the Function Plug-in Wizard installed with MicroStrategy.
For information on R Function Plug-ins, see the open source version of the
Function Plug-in Wizard at http://fpwizard.codeplex.com.
R environment requirements
The following configurations are required when installing R for
MicroStrategy environments that integrate with R analytics as well as
machines that use the deployR utility during R script development to create
an R analytic for use in MicroStrategy:
You must install R version 3.x. The proper version of R must be installed
from http://CRAN.R-project.org:
Windows: The 32-bit version of R is required, regardless of whether
the Windows operating system is a 32-bit or 64-bit version.
can install both the 32-bit version and 64-bit version of R
You
on the same machine.
Unix and Linux: The 64-bit version of R is required.
Installing on Windows
The steps below show you how to use the RScript Functions Installer to
install the R script functions on a Windows environment for MicroStrategy
Analytics Enterprise and MicroStrategy Analytics Desktop.
an early version (1.000.002 and earlier versions) of the R
Upgrading
script functions can fail with a message that a newer version of this
application is already installed. If you encounter this problem while
upgrading, you must first uninstall the existing version of the R script
functions. Refer to your third-party Microsoft documentation for
steps to remove programs from your system. Once the early version of
the R script functions are removed, you can install the new version
using the steps provided below.
To install the R script functions on Windows
9 Select the Re-execute Metadata Repository Upgrade check box for the
Local Host Intelligence Server.
10 Click Next. The Summary page opens.
11 Click Finish to begin the upgrade.
12 Once the upgrade is complete, restart any MicroStrategy Intelligence
Server that is connected to the project metadata that was upgraded. The
R script functions are added to the list of Data Mining Functions available
when creating metrics, including the functions RScript, RScriptU,
RScriptAgg, RScriptAggU, and RScriptSimple.
Once installed, you can launch the deployR utility by typing the following
command in the R Console:
deployR()
11
Graphs and plots can be saved to the file system for inclusion in
MicroStrategy documents and Visual Insight dashboards.
If the directory could not be located using the resources listed above, the
default RScripts folder defined during the R Integration Pack
installation is used. This is typically InstallPath\RScripts.
If the directory could not be located using the resources listed above, the
the R Integration Pack installation folder is used.
Upgrading R, page 22
13
The simple R script for forecasting seasonal data, shown below, will be used
to provide examples of how to implement these best practices to make your R
scripts robust.
Simple R script
#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])
problem executing the R analytic and that you should check the log file for
more details.
steps to define stand-alone metrics as smart metrics, also referred
For
to as smart totals, as well as background information on smart
metrics, refer to the Advanced Reporting Guide.
In both cases described above, a message is logged in the DSSErrors.log
file, unless logging is disabled via the MicroStrategy Diagnostics Utility.
DSSErrors.log is typically found at X:\
The
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:
15
The following example shows how to wrap the forecasting example code in a
tryCatch function to catch errors:
R script with tryCatch
#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)
})
17
The example shown below includes code inserted to adapt the forecasting
script example to generate its own data when it is not executed by
MicroStrategy:
Code to configure dual execution modes
#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])
While there are reasons to avoid the use of a data frame in either flow, such
as if they cause undesired side effects like performance problems, having the
data in the same object for both flows allows for easier comparison.
19
21
Upgrading R
Several times a year, new releases of R become available. The following steps
are recommended to minimize the possibility of problems when upgrading to
a new R release.
To upgrade your R environment
By using all of the recommendations above, the script shown below is more
robust to handle errors, generates PMML, saves important objects from the
R environment for future analysis, and can be executed using the R console
or MicroStrategy:
Robust R script
#tryCatch for Exception Handling
mstr.ErrMsg <- tryCatch({
#Working Directory if executed by MicroStrategy
if(exists("mstr.WorkingDir")) setwd(mstr.WorkingDir)
23
Robust R script
#Check to see if package(s) are installed, install if not and then load
#pkgs is a vector of strings with length >=1
CheckInstallPackages <- function(pkgs){
#For each pkg in pkgs (attempt to load each package one at a time):
x <- lapply(pkgs, function(pkg){
#Load the package if available,
if(!do.call("require", list(pkg))) {
#Silently attempt to install into the default library
try(install.packages(pkg, lib=.Library,repos="http://cran.rstudio.com"))
#Now attempt to load the package, catch error if it wasn't installed
tryCatch(do.call("library", list(pkg)),
#Catch if we're unable to install into the default library
error = function(err) {
#If non-interactive, install into this user's personal library
if(!interactive()) {
#Get the path to this user's personal library
personalLibPath <- Sys.getenv("R_LIBS_USER")
#If the personal library is not in the list of libraries
if(is.na(match(personalLibPath, .libPaths()))) {
#Then create the personal library
dir.create(personalLibPath, recursive = TRUE)
#And add the personal library to the list of libraries
.libPaths(personalLibPath)
}
#Attempt to install the package into the personal library
#If this fails, raise the error back to the report
install.packages(pkg, lib=personalLibPath, repos="http://cran.rstudio.com")
#Finally, attempt to load the package
do.call("library", list(pkg))
}})}})
}
Robust R script
#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])
#If FileName is not an empty string
if(nchar(FileName)>0) {
#Persist objects to file
save(list=c("df", "model", "Forecast"), file=paste(FileName, ".Rdata", sep = ""))
#Load the PMML package
CheckInstallPackages(c("pmml"))
#Save the model as PMML
saveXML(pmml(model), paste(FileName,".xml", sep=""))
}
25
Robust R script
#Print completion 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)
})
The standard workflow for using the deployR utility to capture the signature
of your R analytic and deploy it to MicroStrategy follows.
To deploy R scripts using the deployR utility
27
If this script contains the MicroStrategy header block at the top, then
that information is used to configure the utility and any variables not
identified are displayed in the Unused Variables column.
For new variables, the default Data Type is Numeric and the default
Parameter Type is Vector.
3 Determine the path that is used for the R script using one of the following
methods:
To include the selected R scripts file name and path in the metric
expression, clear the Use R Script Folder check box. When executed,
the specified R script file name and path will be used. If the R script is
not found, execution will fail and an error will be logged, if possible.
To include only the file name of the R script in the metric expression,
select the Use R Script Folder check box. When executed, the R
Script Folder is searched for the specified script. If the R script is not
found, execution will fail and an error will be logged, if possible. The
default location for the R Script Folder is
RIntegrationPackInstallFolder\RScripts. This location is
controlled by the HKLM\SOFTWARE\MicroStrategy\
R Integration Pack\RScriptsFolder registry key.
29
6 You can define the metric that utilizes the R script using the Metric
Specification section, which contains the following options:
Enable Sort By: Controls the sorting of records before the data is
passed to R.
By default, the option is selected. If this option is selected, the first
input must be a vector, since the default behavior sorts records in
ascending order by the first input. To specify a particular sorting
criterion, you can type the sort by value in the field below the check
box.
If this option is cleared, the order of records passed into R is
determined by MicroStrategy automatically.
31
Lines that begin with # are considered comments and are ignored by R.
If you modify the R script or want to change anything else, you can open
the R script using the deployR utility. The deployR utility automatically
restores everything in the header, as well as looks for any new variables in
the script. You can then use the deployR utility to make your
adjustments. Saving any changes replaces the existing header block with
an updated one, and provides the updated metric expression as well.
The metric expression for each output, highlighted with bold text above,
is included in this header block, and is ready to copy and paste into a
metric definition.
If you did not get results, check the DSSErrors.log file for
information on any errors detected.
33
INDEX
A
analytic implementation in R 11
B
best practices
creating a data frame 18
dual execution mode 17
example of a robust R script 23
installing an R package 20
making the R script robust 13
R global environment 22
tryCatch function wrapper 14
upgrading R 22
C
Comprehensive R Archive Network
(CRAN) 10
D
data frame creation 18
deploying an R script 11
deployR utility 3, 10
deploying R scripts 27
using 26
E
examples
dual execution mode 18
R analytic deployment 31
R package installation 20
R script to save the R workspace 17
R script with the tryCatch function 16
robust R script 23
simple R script 14
I
installing
MicroStrategyR Package 10
RScript Functions Installer 5
M
MicroStrategy
documentation 2
Function Plug-in family 1
R Integration Pack 1
35
Index
R
R 1
environment prerequisites 4
global environment 22
installing a package 20
MicroStrategyR Package 3
mstr.ErrMsg variable 14
saving the workspace 16
upgrading 22
variables 19
R Integration Pack 1
components 2
prerequisites 3
R script
adding to an existing project 8
analytic deployment example 31
best practices 13
deploying 11
using the deployR utility 27
example
robust 23
simple 14
folder 28
implementing an analytic 11
parameters 19
RScript Functions 2
36
T
troubleshooting function code using the
tryCatch function wrapper 14
U
upgrading R 22
V
variable
mstr.ErrMsg 14
R 19