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.


R Visualization structure with two data frames initialized


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.


R Script for creating a Lollipop R widget with the swap, sort and rank functionalities


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 NameAction Item
dim_nameDimension dropdown
measure_nameMeasure dropdown
SortsSort order
ArrangesTop/ Bottom
rank_numRank number

R variables with corresponding action item


Configuring the Dropdowns

Let us now add the necessary dropdowns

  1. Swap Dimension
  2. Swap Measure
  3. Sort
  4. 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.


Button onClick event showing script to get and set members to R widget


Default configuration

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.


Application onInitialization event to configure dropdowns and R widget


The table below shows the default values that are set to the R variables.

Script VariableDefault Value
measure_nameVolume 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.

Subscribe to our Newsletter

5920 Windhaven Pkwy
Plano TX 75093.

+1 888-227-2794

+1 972-232-2233

+1 888-227-7192