Skip to Content

Perform an Interactive Data Analysis in SAP Predictive Analytics using Shiny App

Shiny App offers a way to create interactive data analysis applications using R scripts. This blog post will show how to use this interactive analysis capability in SAP Predictive Analytics. In general we utilize a custom R component to hold the Shiny App so it can be added into PA analysis chain. There are steps required to tweak a normal shiny app to become compliant with PA’s requirements on custom R scripts. The remainderof this blog post will show this in more details. Before diving deeper into this topic, let’s have a sneak peak of the final interactive shiny app:



Install shiny app:


Add ShinyApp as PA Custom R Component:

(1) Create a custom R component and give it a name. Here we use “shinyapp_mtcars”.


(2) Add PA compliant shiny app code to the script editor. Normally a shiny app splits its code into two R script files: data analysis logics and plotting logics in server.R, and the ui design in ui.R. Here we need to squeeze server.R and ui.R into one primary function as required by PA. Luckily, shiny app provides a way to define both in runApp. The structure of a PA-compatile shiny app looks like this:

showshiny <- function(mydata){
          ui = fluidPage(
               # UI design code
          server = function(input, output, session){
              # data analysis logics and plotting logics

The parameter mydata is the data frame output from the previous component in the analysis chain. Normally the shinyServer function does not require the third parameter session. But in our case, it is important to have it as this parameter makes sure the shiny app correctly exits when the browser window holding the app is closed. So make sure the session$onSessionEnded function is correctly set up. Make sure the other configurations are also correctly set:


(3) Click Next -> Finish to finish the addition of this custom R component. Now you should be able to see it in the side algorithm panel.

Run PA analysis with a Shiny App component

Using shiny app in PA analysis chain is no different from using a normal custom R component in the analysis chain. In the example shown in this blog post, I use a chain consisting of two analysis nodes: an out-of-box R-Kmeans component and a Shiny App component.


Run the chain and a browser window will pop up containing the Shiny App. Change the value of the scroll bar on the left panel and you will be able to see the plots on the right are also updated. Closing the browser window will return back to PA.


Now we have a close look into this Shiny App. The dataset used here is mtcars. It is loaded into PA as an offline dataset. This analysis chain firstly uses a R-K-Means component to cluster the dataset into three clusters according to features including cylinders, horse power, and weight. Then the shiny app component comes into play for interactive data analysis of the clustered dataset. Basically it does three things: (1) according to the ratio as specified in the slider, it samples the clustered dataset; (2) for data in the sample set, it draws a histogram according to which cluster a car belongs to and plots it (3) for data the in sample set, it fits a linear model to represent the correlation between weight and horse power, then plot the training set and the model. This analysis is only a toy application but I hope this could spark more use cases of practical uses.

Note that to re-run the analysis chain one must update components in the analysis chain first. This could be to remove and add back in the Shiny App component. It can also be changing and changing back the configuration of the K-means component.

Please contact me if you are interested in further discussion on this topic. Source code of the Shiny custom R component used in this blog post is available on request.

You must be Logged on to comment or reply to a post.