MicroStrategy ONE
The R Integration Pack is no longer supported as of December 2024.
Installing Required Packages
R scripts commonly have dependencies on R packages, which are modules that provide additional functionality beyond the out-of-the-box features included with the standard R installation. R packages are installed from CRAN mirror repository sites. The machine executing the script must have access to the Internet to download any missing packages.
If an R 3.x installation is not available from CRAN for your version of Linux, third-party tools such as the Yellowdog Updated, Modified (Yum) and Extra Packages for Enterprise Linux (EPEL) can be used to install and update your R installation. Refer to your third-party R documentation for information on installing and updating R using these tools.
A default CRAN mirror repository site can be established by modifying the following code, by replacing CRANMirrorURL with the desired CRAN mirror URL in the RProfile file located in the /library/base/R directory of the R installation:
options(repos= c(CRAN="CRANMirrorURL"))
For example, you can set http://cran.rstudio.com as the default CRAN mirror repository as shown below:
options(repos= c(CRAN="http://cran.rstudio.com"))
Packages can also be installed manually using the R console, as shown below:
install.packages("RPackage")
Packages need to be installed into the default R library to be available to all users and applications. Administrative privileges are typically required to install into the default R library. Therefore, to install a package into the default R library to provide availability to all users and applications, it is recommended to use an account with administrative privileges and permissions to the default R library to install packages.
When you install packages manually, the R console will prompt you to identify a CRAN mirror repository to use. To avoid this prompt, specify a CRAN mirror repository as part of the manual installation from the R console, as shown below:
install.packages("RPackage",repos="http://cran.rstudio.com")
The sample R script below avoids any required user intervention to install R packages by automatically checking for and installing R packages if necessary. This approach assumes that the machine executing the script has access to the Internet to download any missing packages. For environments that do not have access to the Internet, other workflows need to be used to add any required R packages to the environment.
Installing R packages can require administrative access to folders. The sample R script below includes error handling if the user employing the R script does not have the administrative access required to complete the package installation. In these cases, the sample R script below installs the package in a personal folder for the user that they have access to, or a new folder is created. This error handling for installing packages should be included in R scripts used in MicroStrategy to ensure that users without administrative access can employ the R scripts.
# 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)) }})}})
}
#Load the PMML package
CheckInstallPackages(c("pmml"))
#Save the model as PMML
saveXML(pmml(model), paste(FileName,".xml", sep=""))