Skip to Content

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:

/wp-content/uploads/2015/02/1_646911.png

Prerequisites

Install shiny app:


install.packages("shiny")


Add ShinyApp as PA Custom R Component:

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

/wp-content/uploads/2015/02/2_646912.png

(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){
     library(shiny)
     runApp(list(
          ui = fluidPage(
               # UI design code
          ),
          server = function(input, output, session){
              # data analysis logics and plotting logics
               session$onSessionEnded({
                    stopApp()
               })
           }
     ))
}


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:

/wp-content/uploads/2015/02/3_646985.png

(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.

/wp-content/uploads/2015/02/4_646987.png

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.

/wp-content/uploads/2015/02/5_646991.png

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.

To report this post you need to login first.

14 Comments

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

  1. Laurent Foucault

    Hello!

    I’ve got a small problem using shiny inside Expert Analytics. I have an app that works fine in Rstudio. When I use it in Expert, I get a grey veil on the browser screen and the plot doesn’t appear. If I delete the

    session$onSessionEnded({

            stopApp()

          })

    instruction, the grey veil disappear, but I still don’t get the plot.

    Have you got an idea to solve this? I can leave my code if needed…

    (0) 
  2. Heiko Guessmann

    Hello,

    thank you for showing the use of shiny with SAP PA. I used shiny before and am glad that it works with SAP PA.

    But unfortunately I am having the same problem as Laurent Foucault.

    A browser window opens, but everything is grey. I can only see the titlePanel, but no plot.

    I would be really interested how you got this to work. Could you please provide your code?

    (0) 
  3. Wei Tai Post author

    For some reason it seems the shiny app stops showing the plot area if it is launched in IE. But it works if launched in Google Chrome. For me it works if I keep the IE window open and then start another connection to the Shiny in Google Chrome using the same URL and port number.

    (0) 
  4. Wei Tai Post author

    The example shiny app created for this blog post is still working in Google Chrome but not in IE. I can send out the source code. What is the best way to get in touch with you?      @Heiko @Laurent

    (0) 
  5. Antoine CHABERT

    Hi Laurent, as per SCN policy you should not share email address via post.

    Please do so through your SCN profile and edit the post.

    Thanks for your understanding.

    Antoine

    (0) 
    1. Antoine CHABERT

      Hi, your address is no longer visible to Wei Tai. Please make it visible in your SCN profile, using the privacy options. Another option is you follow each other, you can address private messages (socalled direct messages in SCN jargon).

      Hope this helps

      Antoine

      (0) 

Leave a Reply