This blog series is about leveraging R Visualizations in SAP Analytics Cloud. In the previous blog, we learnt how to add interactivity and customizations to R widgets. In this blog, we will see how to swap dimensions/measures, sort and rank visualizations dynamically in SAP Analytics Cloud using R.
Initializing the Data Frame
Add an R Visualization to the canvas and create a data frame as required. For the steps to create a data frame, visit this blog.
For the sake of simplicity, let us consider the dimensions and measures highlighted in the snapshot below to demonstrate swap, sort and rank at runtime. You can also include various other options like adding dimensions/measures, flipping axis, etc. per your needs.
Configuring the R widget
The visualization uses ggplot2 and dplyr libraries with standard evaluation (SE) semantics to achieve dynamic parameter passing from the SAP Analytics Cloud environment. Let us create a Lollipop chart using R to visualize the data, although any chart type can be used. Follow the script in the snapshot below, to have the widget configured.
Note how geom_segment() along with geom_point() are used in combination to create a Lollipop chart. You can play around with the script and then click on Apply to add the visualization to the canvas.
The R variables used in the script are described in the table below. They must be declared/defined in the onClick and onInitialization events of the analytic application, to be used within the R script.
These variables act as intermediates between the SAP Analytics Cloud environment and the R script using setString() and setNumber() functions to set the R variables from the respective dropdowns.
|Variable Name||Action Item|
R variables with corresponding action item
Configuring the Dropdowns
Let us now add the necessary dropdowns
- Swap Dimension
- Swap Measure
- Show Top / Bottom
The snapshot below shows the layout of the dropdowns, input field and button on the canvas.
After adding the dropdowns, the onClick event of the Apply button is configured to get user selections from the respective dropdowns and set them to the R widget. The following snapshot shows the script for the same.
Note that if the dimension/measure names have spaces in them, they need to be enclosed in ” `” as seen in line no. 7 in the script below.
All the dropdowns and the input fields are configured in the onInitilization event of the application to have default values. The application is set to show the top five liquor Category based on Volume Sold (Liters) sorted in ascending order, as seen below.
The table below shows the default values that are set to the R variables.
|Script Variable||Default Value|
|measure_name||Volume Sold (Liters)|
Initial values for R variables
When the application is run, the visualization is rendered with the default options set as seen below.
In the next blog of this series, we will look at how to swap dimensions/measures, sort and rank visualizations dynamically.
Reach out to our team here if you are interested in evaluating if SAP Analytics Cloud is right for you.