SAP Analytics Cloud Story Supplemented by R Animation
When it comes to visualization, animated charts can be very appealing by showing historical trends and movements. Currently animated visuals can be natively created in SAP Analytics Cloud Applications using Timer: “Using Timer in Your Analytic Applications“. However if we would like to avoid tweaking data filters for every frame of the animation, and therefore causing data refreshing, SAP Analytics Cloud provides R widget support, with which animated charts can be easily created in R and consumed in a Story.
This blog will use the most frequently visualized data these days at the time of writing, COVID-19 cases. It will walk through the steps to create a animated map showing development of total cases around the world over the last five months. I would assume you possess some basic knowledge to create SAP Analytics Cloud Models and Stories.
Our World in Data provides a day-by-day dataset of worldwide COVID-19 cases. It is updated daily and has both daily new cases and cumulated total cases for each country. There is a data quality issue that we will later address: missing dates in the time series. Data can be accessed at:
We will upload the csv file and create an Import model.
We will have a model with a preview like this.
I saved it with the name ‘COVID-19-OWID‘.
Since the dataset is small, we will deal with the data quality issue later directly in powerful R: it can fix the issue in a couple of lines.
Time to create the story!
Let’s go straight to the point: adding following R code to script section. Its logic is explained in embedded comments.
# We use tidyr to cleanse and transform data library(tidyr) # We use plotly to generate the chart library(plotly) # Source Data Model COVID_19_OWID %>% #Remove records that show 0 total cases filter(total_cases != 0) %>% #Generate a full permutation of countries and dates, filling total_case as NA for any missing records mutate(iso_code = factor(iso_code), location=factor(location), date=factor(date)) %>% complete(nesting(iso_code, location), date, fill = list(total_cases = NA)) %>% # If, for a country, certain dates are missing in the middle, total_cases is then carried forward from a previous date group_by(location) %>% fill(total_cases, .direction="down") %>% ungroup() %>% #Data is ready, continue to plot plot_geo(locationmode="ISO-3") %>% #Scale is capped at 200,000 cases, making it easier to differentiate countries with smaller numbers add_trace(z=~total_cases, locations=~iso_code, frame=~date, colorscale="Reds", reversescale=FALSE, zmin=0, zmax=200000) %>% layout(geo=list(scope="world"), title=~date) %>% #Frame rate is set to 50ms. Smaller number will result in a faster playback animation_opts(frame=50, mode="immediate")
Click on ‘Play’ button and watch it live!
If the R scipt shows any error, please check these:
- If you used a different Model name, please replace the name at line 8 of R script
- Make you your Rows/Columns settings are same as the screenshot above
- Test you R runtime by running a simple command: version
Some most frequently asked questions are:
Q: Can we bypass ‘Play’ button so that the chart can playback and repeat itself automatically?
A: Depends. The functionality is available in package ‘gganimate’. However it is not installed in default SAP R server runtime environment. If you happen to be using your own remote R connection, install ‘gganimate’ and rewrite R code. Just be mindful some interactive features provided by plotly, such as tooltips, will no longer be available.
Q: How about live data connection?
A: Your system administrator needs to switch it on for you.
This blog illustrated a simple example on how R widgets can help enrich SAP Analytics Cloud Stories. R widgets provide a great addition to the standard SAP Analytics Cloud charts library. They are particularly suitable in those scenarios where highly specialized visualizations are needed. Furthermore, the abundance of R libraries forms a perfect complement to the powerful SAP Analytics Cloud and SAP HANA Platform.