Power BI Desktop enable users to use R script visual for adding custom visualization generated with R language – regardless of R package used. Before using R script visual, you will need to enable it by setting path to R Engine on your client in the global options. Once this is done, you will be able to enhance your Power BI reports using R visualizations.
My personal favorite R packages I like to use in Power BI are shiny, plotly, ggplot2, googleVis, ggiraph, rCharts and d3js. Apart from finding the package for data visualization where you feel home, one of the very great thing I find with Power BI + R is the ability to have visualizations created dynamically (more or less like if you would be using plotly or shiny) with slicers and selectors. And this makes data exploration, data wrangling so much easier.
With October 2016 Power BI custom visuals got additional 7 R visuals to the family (check Power BI Visuals Gallery and fall in love with your favorite one 🙂 ).
Upon using any of the additional R visuals, Power BI will – prior to importing the additional visualizations – ask for installing all the missing packages, as well as inform you of any incompatibility between R engine and package.
So how easy is to create R visualization in Power BI? Let us first import data to Power BI from SQL Server. I have generated a sample data-set for this demo. Data and all the samples are available at Github.
With sample data-set I have created a simple data visualization with slicers using Power BI.
Now, the same visualization using R.
Note that I have used same slicers (Year = 2016, Month = 2) and R graph (using ggplot2) was able to dynamically visualize the numbers. So this mean, that any R visualized graph can be navigated and dynamically updated, which is absolutely great, especially in cases when you do data exploration. Following R code was used:
library(ggplot2) ggplot(data=dataset, aes(x=Day, y=Value)) + geom_line(aes(color="red")) + ggtitle("Value by day") + theme(legend.position = "none") + theme(panel.background = element_rect(fill = "white", color = "lightgray", size = 1))
with additional dataset conversion to data.frame done automatically.
Where it get’s tricky is if you want to return the R data.frame into your Power BI page in form of a table (imagine you want some results to your graph or that you are calculating some statistics). Well, Power BI does not allow returning tables from R script visual. Unless, table is an image (that is, in a “form” of a visualized “graph”).
So I manage to create a table as a graph using R and gplot function from ggplot2 package.
The best part of this table is, that is dynamically changed based on selection on graphs or slicers. The downside is, that it can not be scrolled within the object. So if you end up with bigger table, you have to tweak the font size or show table aggregated or grouped. In my opinion, I would not change R created table with Power BI table, because Power BI table is simply better and easier to create and maintain. But I would definitely recommend using R tables to support your R visuals. For example: If you are generating clusters using R in power BI, use R generated table to expose additional statistics on clusters.
The code for generating this R table is:
library(gridExtra) library(ggplot2) qplot(x=1:5, y=1:5, geom = "blank") + theme(line = element_blank(), text = element_blank()) + annotation_custom(grob=tableGrob(dataset))
Script generated blank plot, where custom annotation is added with function grob. And this is our data.frame from dataset and is displayed on top of empty graph.In addition to “remove” the background blank graph, blank line and text is added:
theme(line = element_blank(), text = element_blank())
Update #1 (19.12.2016)
After reading the comment to this blog post from reader Baptiste Auguie (with link to his post on displaying tables as grid graphs), that using grid will return the same result, but very much a simpler solution to plotting / drawing table. Thank you Baptiste! I must have missed this one.
R code is far less simpler without qplot function.
library(gridExtra) grid.table(dataset)
And the printscreen from Power BI:
As always, sample, code and data-set is available at Github.
Happy PowerBI-Rrrring!
[…] leave a comment for the author, please follow the link and comment on their blog: R – TomazTsql.R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data […]
LikeLike
Hi Tomaz,
I saw you mentioned ggiraph as one of the packages you use in Power BI.
Can you please help me, is not working for me…
library(ggplot2)
library(ggiraph)
p <- ggplot(dataset, aes(x = gdpPercap, y = lifeExp)) +
geom_point_interactive() +
facet_wrap(~continent)
ggiraph(code = print(p))
LikeLike
Sorry, this is the code I used:
library(ggplot2)
library(ggiraph)
p <- ggplot(dataset, aes(x = gdpPercap, y = lifeExp)) +
geom_point_interactive(aes(tooltip = country)) +
facet_wrap(~continent)
ggiraph(code = print(p))
LikeLike
[…] article was first published on R – TomazTsql, and kindly contributed to […]
LikeLike
please don’t use a dummy ggplot to draw a table, that’s nonsense. `grid::grid.draw()` is the right way to do it
LikeLike
Thank you! for this.
I will update blog post using only:
library(gridExtra)
grid.table(dataset)
Which does the same and sure is less complicated.
LikeLike
Thanks for the wonderful tutorial. I have heard that Power BI allows only up to 100,000 rows of data to be plotted using R? Any comment on this?
LikeLike
[…] article was first published on R – TomazTsql, and kindly contributed to […]
LikeLike
Reblogged this on Power Blogger BI.
LikeLike
[…] You can also combine custom R charts with other Power BI tools to create interactive R charts. A favorite of mine is the Timeline slicer, which allows you to select a range from a date variable. (The dashboard, including the R charts, is updated using only data from the date range you select.) You can also use the built-in date slicer for similar effect, and Tomaž Kaštrun demonstrates how to combine it with a custom R chart in his blog post R graphs and Tables in Power BI Desktop. […]
LikeLike
[…] You can also combine custom R charts with other Power BI tools to create interactive R charts. A favorite of mine is the Timeline slicer, which allows you to select a range from a date variable. (The dashboard, including the R charts, is updated using only data from the date range you select.) You can also use the built-in date slicer for similar effect, and Tomaž Kaštrun demonstrates how to combine it with a custom R chart in his blog post R graphs and Tables in Power BI Desktop. […]
LikeLike
[…] You can also combine custom R charts with other Power BI tools to create interactive R charts. A favorite of mine is the Timeline slicer, which allows you to select a range from a date variable. (The dashboard, including the R charts, is updated using only data from the date range you select.) You can also use the built-in date slicer for similar effect, and Tomaž Kaštrun demonstrates how to combine it with a custom R chart in his blog post R graphs and Tables in Power BI Desktop. […]
LikeLike
[…] You can also combine custom R charts with other Power BI tools to create interactive R charts. A favorite of mine is the Timeline slicer, which allows you to select a range from a date variable. (The dashboard, including the R charts, is updated using only data from the date range you select.) You can also use the built-in date slicer for similar effect, and Tomaž Kaštrun demonstrates how to combine it with a custom R chart in his blog post R graphs and Tables in Power BI Desktop. […]
LikeLike
You should use grid.draw instead of grid.table. It’s much faster and easier to format:
http://stackoverflow.com/questions/31796219/grid-table-and-tablegrob-in-gridextra-package
LikeLike
[…] R graphs and tables in Power BI Desktop tomaztsql.wordpre… […]
LikeLike
I am trying to use this recommendation i.e. gridExtra::grid.table(df) in PBI, but display is different than what I get using the same data and the same code directly in RStudio. I wish I could attach screenshots, but just as an example I have a constant column with some text (A1020390D71), which is displayed correctly in RStudio, but in PBI its is displayed as 1, column with some numbers (e.g. 175, 2481… and other 0.486, 0.3…) and these are displayed as either “1” or “0” constant columns…
What could be a problem? Can I copy “R data frame” to PBI table and display that instead of using grid.table() in R script?
LikeLike
Hi. Can you send the dataframe sample on email? And I can look it up? Thanks, Tomaz
LikeLike
Hi Tomaz,
Thank you for looking into this. Please find attached:
– two screenshots (from RStudio and PBI) – R script I am using – CSV file with data
It seems to me that the problem might have to do with Power BI doing something with data types in date columns, but other than that I have no idea why this is happening.
All the best, R
Ryszard Czerminski 508-358-6328 ryszard@czerminski.net LinkedIn.com/in/Ryszard.Czerminski
LikeLike
Hi – I’m having trouble using different fonts with R & Power BI. The different font shows up in my Desktop Power BI, but not when I publish to Power BI Service. Do you if there is a solution?
LikeLike
Hi,
Check your regional settings. You can alao use custom power bi fonts in .tffs format and should render the same. Extra caution with mobile version and viewing of power bi.
In addition, you can use custom theme and set the fonts there as well.
Best,Tomaz
LikeLike