Reader small image

You're reading from  Extending Power BI with Python and R - Second Edition

Product typeBook
Published inMar 2024
Reading LevelIntermediate
PublisherPackt
ISBN-139781837639533
Edition2nd Edition
Languages
Right arrow
Author (1)
Luca Zavarella
Luca Zavarella
author image
Luca Zavarella

Luca Zavarella has a rich background as an Azure Data Scientist Associate and Microsoft MVP, with a Computer Engineering degree from the University of L'Aquila. His decade-plus experience spans the Microsoft Data Platform, starting as a T-SQL developer on SQL Server 2000 and 2005, then mastering the full suite of Microsoft Business Intelligence tools (SSIS, SSAS, SSRS), and advancing into data warehousing. Recently, his focus has shifted to advanced analytics, data science, and AI, contributing to the community as a speaker and blogger, especially on Medium. Currently, he leads the Data & AI division at iCubed, and he also holds an honors degree in classical piano from the "Alfredo Casella" Conservatory in L'Aquila.
Read more about Luca Zavarella

Right arrow

Interactive R Custom Visuals

In our journey through the fascinating world of data visualization, we most recently arrived at Chapter 21, Advanced Visualizations, and discovered the amazing potential to construct intricate and visually compelling graphs, such as circular bar plots, thanks to the exceptional flexibility offered by ggplot. However, as we navigated these waters of complex graphing, an underlying question may have arisen – are we exploiting the full potential of the data by simply presenting it in these imaginative graphs? Or are we somehow limited by the lack of user interaction?

The feeling is not unfounded. One could argue that an inherent limitation of static charts is their lack of responsiveness to user input, in the form of tooltips and other interactive features. This missing element has the potential to limit the way we understand and interpret data from these visualizations.

To further deepen your understanding and ability to create engaging and...

Technical requirements

This chapter requires you to have a working internet connection and Power BI Desktop installed on your machine (version: 2.123.742.0 64-bit, November 2023). You must have properly configured the R engine and IDEs as outlined in Chapter 2, Configuring R with Power BI.

Why interactive R custom visuals?

Let’s start with a graph you’ve already implemented in R. For example, consider the raincloud plot of Fare versus Pclass variables introduced in Chapter 19, Exploratory Data Analysis (this time not grouped by Sex):

A screenshot of a graph  Description automatically generated

Figure 22.1: Raincloud plot for Fare (transformed) and Pclass variables

Focus for a moment just on the boxplots you see in Figure 22.1. Although the Fare variable is already transformed according to Yeo-Johnson to try to reduce skewness, there are still some extreme outliers for each of the passenger classes described by the categorical variable Pclass. For example, if you wanted to know the values of the transformed variable Fare that correspond to the whiskers (fences) of the boxplot on the left so that you could then identify the outliers that are beyond these whiskers, it would be convenient to have these values appear when you hover the mouse near this boxplot. It would be even more interesting to know the...

Adding a dash of interactivity with Plotly

There is an open source JavaScript library for data visualization, which is declarative, high-level, and allows you to create dozens of types of interactive graphs called Plotly.js. This library is the core of other Plotly client libraries, developed for Python, Scala, R, and ggplot. In particular, the library developed for R, called Plotly.R (https://github.com/ropensci/plotly), provides the ggplotly() function, which does all the magic for us: it detects all the basic attributes contained in an existing graph developed with ggplot and transforms it into an interactive web visualization. Let’s see an example.

First, you need to install the Plotly.R library (https://github.com/ropensci/plotly) on your latest CRAN R engine using the install.packages('plotly') script.

IMPORTANT NOTE

For simplicity, we’ll make sure to run the custom visual on the latest version of the CRAN R engine, since all the...

Exploiting the interactivity provided by HTML widgets

HTML widgets are R packages that allow you to create interactive web pages. These packages are generated by a framework that creates a binding between R and JavaScript libraries. This framework is provided by the htmlwidgets package developed by RStudio. HTML widgets are always hosted inside an R package, including the source code and dependencies, to ensure that the widgets are fully reproducible even without access to the Internet. For more details on how to develop an HTML widget from scratch, see the references.

In addition to being able to embed HTML widgets in RMarkdown files (dynamic documents with R) or Shiny applications (interactive web applications built directly from R), the htmlwidgets package allows you to save them also in standalone web page files thanks to the saveWidget() function.

However, there are hundreds of R packages that expose their functionality in HTML widgets. You can browse the htmlwidgets...

Packaging it all into a Power BI custom visual

Power BI Visual Tools (pbiviz) are the easiest way to create custom visuals in Power BI. They are written in JavaScript (using Node.js) and are used to compile the source code of .pbiviz packages. A .pbiviz package is a zipped version of the Power BI visual project, which in turn is a set of folders, scripts, and assets needed to create the custom visualization you want to implement. In general, a standard Power BI visual project is created from a template using the pbiviz command-line tools. The contents of the template depend on the method you want to use to create the custom visual (TypeScript, R visual, or R HTML).

IMPORTANT NOTE

The pbiviz tools do not support any technology that uses Python behind the scenes, such as the ipywidget widgets.

With this in mind, it is worth learning a little more about R and ggplot to be able to develop interesting custom visuals using the R visual and R HTML modes. In addition...

Importing the custom visual package into Power BI

Now that the bulk of the work is done, importing your custom visual into Power BI is a breeze. First of all, you need to install the xml2 package in your R engine, as it is used by the provided utility functions:

  1. Open RStudio and make sure it is pointing to your latest CRAN R (version 4.2.2 in our case).
  2. Click on the Console window and type this command: install.packages('xml2'). If you remember, this library is listed in the dependency file you saw in the previous section. Then, press Enter.

Now let’s import the custom visual into Power BI:

  1. Make sure that Power BI Desktop is pointing to the correct R engine (the latest one) in Options.
  2. Click on Get data, search for web, select Web, and click on Connect.
  3. Type the following URL as the source: http://bit.ly/titanic-dataset-csv. Then click OK.
  4. Make sure that the File Origin is 65001: Unicode (UTF-8) and click Load...

Summary

In this chapter, you learned about the advantages of using an interactive visual over a static visual in some cases. You learned how to add some basic interactivity to charts developed with ggplot using Plotly.

You learned that the key to creating interactive visuals in Power BI is that they are based on HTML widgets. So you were guided step by step through the creation of a custom visual, compiled using the pbiviz tools.

Finally, you imported the compiled package into Power BI to test its functionality.

As we close this edition, we reflect on the transformative journey we’ve been on together. In addition to adding advanced analytics to your toolbox, you’ve mastered the art of combining the robust capabilities of Power BI with the analytical power of Python and R. With these skills, you’ll be able to go beyond traditional data analysis limitations and turn complex data into powerful stories and predictive insights. This book has been designed...

References

For additional reading, check out the following books and articles:

Test your knowledge

  1. Why choose interactive visuals over static charts?
  2. How does the chapter suggest adding interactivity to ggplot graphs?
  3. How can a user create an interactive Power BI custom visual with R?
  4. Is it possible to publish standard R visuals to the web using the Publish to Web feature?
  5. Is it possible to use Python with pbiviz to create interactive custom visuals?

Learn more on Discord

To join the Discord community for this book – where you can share feedback, ask questions to the author, and learn about new releases – follow the QR code below:

https://discord.gg/MKww5g45EB

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Extending Power BI with Python and R - Second Edition
Published in: Mar 2024Publisher: PacktISBN-13: 9781837639533
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Luca Zavarella

Luca Zavarella has a rich background as an Azure Data Scientist Associate and Microsoft MVP, with a Computer Engineering degree from the University of L'Aquila. His decade-plus experience spans the Microsoft Data Platform, starting as a T-SQL developer on SQL Server 2000 and 2005, then mastering the full suite of Microsoft Business Intelligence tools (SSIS, SSAS, SSRS), and advancing into data warehousing. Recently, his focus has shifted to advanced analytics, data science, and AI, contributing to the community as a speaker and blogger, especially on Medium. Currently, he leads the Data & AI division at iCubed, and he also holds an honors degree in classical piano from the "Alfredo Casella" Conservatory in L'Aquila.
Read more about Luca Zavarella