This series is about leveraging R Visualizations in SAP Analytics Cloud. In this blog, let us take a look at creating R widgets in an analytic application and performing dynamic filtering on them.
Advantages of R Visualizations in SAP Analytics Cloud
R visualizations are great for creating plots used for exploratory data analysis. R being an open source language, it has countless libraries which allow developers to extensively customize charts starting from UI to the underlying data as per the reporting needs. It is highly flexible and easy to use. For more on R, visit this link.
R visualizations in SAP Analytics Cloud can interact with other SAC components just like any other standard chart. The image below shows a standard column chart in SAC and a column chart created using R.
Dynamic Filtering in R Visualizations
Let us see how to dynamically filter R visualizations. Here, a simple column chart is used. Shown below is the Dynamic filtering of R Visualization in action:
The input to R visualizations is a Data Frame. A Data Frame is simply like a table with rows and columns where the structure of the table is built based on a list of fields.
Create an R visualization on the canvas. In the Builder tab of the R visualization, input data (data frame) can be added using the Add Input Data option.
For example, add a generic sales data source as input data to the R visualization.
Add the Category dimension to the rows and Volume Sold (Liters) measure to the columns. You can see a preview of the selected rows and columns from the data source on the right side of the input data pane. Click OKto save the structure. Now the Data Frame (Liquor_Sales_Data) is ready to be used for the R visualization.
Adding R Script
After the initial Data Frame has been set up, use the Edit Script option from the Builder pane to set up R script to visualize the data.
Add the following script to create a column chart:
The script includes the library ggplot2as it is elegant and easy to use. You can use any library as required. You can learn more about ggplot2 here.
The Data Frame is assigned to a local variable dFrame. The function ggplot renders the column chart with geom_bar(),assigning Category to x-axis and Volume Sold (Liters) to y-axis using the ggplot2::aes()function.
The script pane also displays a preview of the code executed. The developer has the liberty to play around with R scripts here and view the results directly using the Execute command button. When done, click on the Apply button for adding the R visualization to the canvas.
Filtering R Visualization
Add a dropdown that would filter dimension Packs from the data model.
1. Configuring the Dropdown
To populate the members, include the following script in the onInitialization() function of the application.
Here the function getMembers() is used to get the list of packs. And the function addItem() is used within a loop to populate the members in the dropdown.
2. Dynamic filtering
Now add the following script in the onSelect() function of the DropDown to achieve dynamic filtering functionality.
Note that you need to get the Data Frame of the R widget using getDataFrame() before you access its data source using getDataSource(), whereas, in the standard SAC chart, the data source can be directly accessed using getDataSource().
Now you will be able to dynamically filter the R visualization.
* * *
In the subsequent blogs, we will learn more about customizing and adding interactivity to R Visualizations in SAP Analytics Cloud.
Reach out to our team here if you are interested in evaluating if SAP Analytics Cloud is right for you.