R Visuals or Custom Visuals in Microsoft Power BI?

R Visuals or Custom Visuals in Microsoft Power BI?
Reading Time: 5 minutes

Over the past few years, there has been a movement in R to create packages that bind to JavaScript libraries to make R more interactive. Notably, htmlwidgets and rCharts are two examples that come to mind. In an IDE like R Studio or a framework such as Shiny, the enhanced visualization appears and functions like it would if you had created it using JavaScript. Instead of coding it in JavaScript though, everything is done in R.

What if it were possible to do something similar using the R visual in Microsoft Power BI? For example, perhaps we could create a simple Leaflet map using R’s Leaflet package? The image below uses the R visual in Power BI — not a custom visual.

e0c66b374472bc8639bb1ef2f58d00e7

 

I first explored how to create more flexible maps in Power BI with my “Enhanced Map” for the Power BI Best Visual Contest back in September. That custom visual used Leaflet to move beyond Power BI’s default Map and Filled Map. It was not a production-ready attempt, but it was a decent prototype. With the advent of the R visual in Power BI, however, it is now possible to quickly code static visualizations that otherwise would take substantially longer to create as custom visuals.

 

Should You Create R Visuals or Custom Visuals?

When considering whether to create a custom visualization in R versus TypeScript, there are a number of factors to weigh. Given enough time (and if you have the ability to code it), I would generally recommend a TypeScript visual for a variety of reasons. Does that discount using the R visual? No. In many cases, using R to create visuals has a large advantage.

One big trade-off for the R visual versus a custom visual is the lack of interactivity within the R visual itself. Power BI Desktop currently requires a static image as output for the R visual (please vote to change that here). That’s fine for ggplot2 and other static plots, but it is a problem when you attempt to use a package such as htmlwidgets. For example, I cannot zoom or pan the Leaflet map in the R visual that I created.

In my opinion though, even a static Leaflet map beats the default “slippy” Power BI maps in their current state. No contest. Customization such as easily swapping out base tiles, changing point markers, and other tweaks all help to improve the final experience and make the map stand out compared to a similar attempt using a default map. You would get that in either the R visual or a custom visual. Also, if you combine the R visual with other Power BI charts for filtering the data, you get a certain level of interaction even if the map itself does not zoom or pan.

Leaflet R mapLeaflet Example.PNG

Comparable default Power BI mapBing Example.PNG

Another trade-off for the R visual versus a custom visual is reduced portability with R. Rather than simply importing a custom visual file in PBIViz format and being able to drag fields into the field well, it takes more effort to adapt the R code to your unique data.

Even simple conveniences like tooltips are not possible with the R visual.

On the other hand, a major advantage of the R visual versus a custom visual in my opinion is the time to develop. Whereas it may take a few minutes to tweak the R visual, it can take hours to create and tweak a TypeScript custom visual. Perhaps more important, most Power BI developers come from an analyst or business intelligence background rather than one in web development. TypeScript, JavaScript, and D3 are not common skills for BI developers. In this case, it may take some work to learn how to code R visuals, but R has a significantly lower barrier to entry than creating a TypeScript custom visual for most people. Even if the output is static instead of interactive, it can still be used to great effect.

 

The Issue with R + JavaScript Output

Unfortunately, creating a map or other plot using the various R+JavaScript libraries is not as straightforward as creating a static plot with the R visual. That is because the output for those enhanced R visualizations is HTML and not an image. In Power BI Desktop, if we attempt to render a map using R’s Leaflet package, the code might look something like this:

library(leaflet)
customPalette <- colorFactor(c("yellow", "red", "green", "#00274c"), domain = dataset$OwnerType)
m <- leaflet(data = dataset) %>%
    setView(-85.5, 42.3, zoom = 6) %>%
    addProviderTiles("CartoDB.Positron") %>%
    addCircleMarkers(~Longitude, ~Latitude, radius = ~ifelse(OwnerType == "Private", 1, 3), color = ~customPalette(OwnerType), stroke = FALSE, fillOpacity = 0.8)
print(m)

 

That code as well as other examples that render without issue in R Studio will not work when adapted to Power BI Desktop. Power BI expects an image as output for the R visual, and without one, it provides the following error:

Cannot Display - No Image Created

 

Given that constraint, what if we attempt to create a jpeg or png file and print/plot to that file? In effect, we are trying to take a “screenshot”. In that case, the code might look something like this:

library(leaflet)
customPalette <- colorFactor(c("yellow", "red", "green", "#00274c"), domain = dataset$OwnerType)
png(filename = "failure.png", width = 800, height = 600)
m <- leaflet(data = dataset) %>%
    setView(-85.5, 42.3, zoom = 6) %>%
    addProviderTiles("CartoDB.Positron") %>%
    addCircleMarkers(~Longitude, ~Latitude, radius = ~ifelse(OwnerType == "Private", 1, 3), color = ~customPalette(OwnerType), stroke = FALSE, fillOpacity = 0.8)
print(m)
dev.off()

The effort to create a PNG would also result in failure, and Power BI would simply display an empty white image, not the desired map or plot rendered properly.

 

The Solution

How can someone adapt R packages such as htmlwidgets for use in Power BI? If you lose interactivity within the visual itself, is it even worth having a static image? Given the significant time saved using the R visual over attempting to do so with a custom visual, I would argue that it is worthwhile in many cases. It takes a bit of additional work beyond the basic R visual, but I will explore that soon in a tutorial.

 




7 Comments

  1. Hi David, thanks for the post. I am trying to accomplish this same thing in PowerBI using leaflet. Stuck at the same point. I was hoping maybe you had a solution. The leaflet map is very nice and I would love to use it.

    1. Leaflet for R visual or custom visual? I have a solution for R, but it requires NodeJS and is still only a static image. My leaflet custom visual needs a lot more work, but I haven’t put much time into it recently. James Dales created a Leaflet custom visual for Altius BI, but I do not believe it’s openly licensed or publicly available.

Leave a Reply

%d bloggers like this: