Created
October 28, 2016 17:56
-
-
Save wrschneider/f7613a5dcddac91c209315a9c6f9117c to your computer and use it in GitHub Desktop.
call R qicharts package from Microstrategy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#MICROSTRATEGY_BEGIN | |
# | |
#DESC This script takes a metric from MSTR as a vector, and passes it to qicharts. Then returns the LCL, CL or UCL as | |
# a metric for use in MSTR. | |
#DESC Revision A | |
# | |
#RVAR Metric -input -numeric -vector -repeat | |
# | |
#RVAR Chart -parameter StringParam8 | |
# | |
#RVAR CL -output -numeric -vector | |
#RVAR LCL -output -numeric -vector | |
#RVAR UCL -output -numeric -vector | |
# | |
#MICROSTRATEGY_END | |
# Example usage: 'u' chart that requires two metrics for both y and n | |
# RScript<_OutputVar=UCL, _RScriptFile="C:\users\schneidb\Downloads\qicharts.r", _Params="Chart='u'">([metric1], [metric2]) | |
# | |
# 'c' chart that requires only one metric | |
# RScript<_OutputVar=UCL, _RScriptFile="C:\users\schneidb\Downloads\qicharts.r", _Params="Chart='c'">([metric1]) | |
# | |
# Uses "repeat" for inputs to allow for variable number of inputs (y only, or y and n) | |
mstr.ErrMsg <- tryCatch({ #tryCatch for Exception Handling | |
if(exists("mstr.WorkingDir")) setwd(mstr.WorkingDir) #Working Directory if executed by MicroStrategy | |
#Check to see if package(s) are installed, install if not and then load | |
CheckInstallPackages <- function(pkgs) { #pkgs is a vector of strings with length >= 1 | |
x <- lapply(pkgs, function(pkg){ #For each pkg in pkgs (attempt to load each package one at a time): | |
if(!do.call("require", list(pkg))) { # Load the package if available, | |
try(install.packages(pkg, lib=.Library, | |
repos="http://cran.rstudio.com")) # Silently attempt to install into the default library | |
tryCatch(do.call("library", list(pkg)), # Now attempt to load the package, catch error if it wasn't installed | |
error = function(err) { # Catch if we're unable to install into the default library | |
if(!interactive()) { # If non-interactive, install into this user's personal library | |
personalLibPath <- Sys.getenv("R_LIBS_USER") # Get the path to this user's personal library | |
if(is.na(match(personalLibPath, .libPaths()))) { # If the personal library is not in the list of libraries | |
dir.create(personalLibPath, recursive = TRUE) # Then create the personal library | |
.libPaths(personalLibPath) # And add the personal library to the list of libraries | |
} | |
install.packages(pkg, lib=personalLibPath, # Attempt to install the package into the personal library | |
repos="http://cran.rstudio.com") # if this fails, raise the error back to the report | |
do.call("library", list(pkg)) # Finally, attempt to load the package | |
} | |
} | |
) | |
} | |
}) | |
} | |
#Calculation | |
if (!exists("Chart")) { | |
Chart <- "c" | |
} | |
CheckInstallPackages("qicharts") | |
# write.csv(Metric, "output.txt") | |
# because of "repeat" Metric will be a matrix rather than a one-dimensional vector | |
df <- as.data.frame(Metric) | |
result <- qic(y=df$V1, n=df$V2, chart = Chart) | |
# write(result$lcl, file="output2.txt") | |
UCL <- result$ucl | |
CL <- result$cl | |
LCL <- result$lcl | |
#Finish | |
try(print("Success!")) #Print completion message when run from the console | |
mstr.ErrMsg <- "" #If we made it here, no errors were caught | |
}, error = function(err) { #Catch block to report an error | |
try(print(err)) # Print error message to console (using try to continue on any print error) | |
return(err$message) # Return error Message | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment