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.
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 map
Comparable default Power BI map
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.
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:
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.
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.