R graphs and tables in Power BI Desktop

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.

1450757873470

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 🙂 ).

2016-12-18-09_26_55-visuals-gallery-_-microsoft-power-bi

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.

2016-12-18-11_32_26-preparing-data-sql-sicn-kastrun-plan-spar_si01017988-69_-microsoft-sql-s

With sample data-set I have created a simple data visualization with slicers using Power BI.

2016-12-18-11_34_01-sample_powerbi_r-power-bi-desktop

Now, the same visualization using R.

2016-12-18-11_34_01-sample_powerbi_r-power-bi-desktop_2

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.

2016-12-18-11_57_51-sample_powerbi_r-power-bi-desktop_code

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.

2016-12-18-12_40_32-sample_powerbi_r-power-bi-desktop

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:

2016-12-19-06_13_19-sample_powerbi_r-power-bi-desktop

As always, sample, code and data-set is available at Github.

Happy PowerBI-Rrrring!

Tagged with: , , , , , ,
Posted in Uncategorized
20 comments on “R graphs and tables in Power BI Desktop
  1. […] 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 […]

    Like

    • Orlando says:

      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))

      Like

      • Orlando says:

        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))

        Like

  2. […] article was first published on R – TomazTsql, and kindly contributed to […]

    Like

  3. please don’t use a dummy ggplot to draw a table, that’s nonsense. `grid::grid.draw()` is the right way to do it

    Like

  4. tomaztsql says:

    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.

    Like

  5. K Imran M says:

    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?

    Like

  6. […] article was first published on R – TomazTsql, and kindly contributed to […]

    Like

  7. […] 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. […]

    Like

  8. […] 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. […]

    Like

  9. […] 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. […]

    Like

  10. […] 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. […]

    Like

  11. ironryan77 says:

    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

    Like

  12. […] R graphs and tables in Power BI Desktop tomaztsql.wordpre… […]

    Like

  13. Ryszard Czerminski says:

    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?

    Like

    • tomaztsql says:

      Hi. Can you send the dataframe sample on email? And I can look it up? Thanks, Tomaz

      Like

      • Ryszard Czermiński says:

        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

        Like

  14. John says:

    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?

    Like

    • tomaztsql says:

      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

      Like

Leave a comment

Follow TomazTsql on WordPress.com
Programs I Use: SQL Search
Programs I Use: R Studio
Programs I Use: Plan Explorer
Rdeči Noski – Charity

Rdeči noski

100% of donations made here go to charity, no deductions, no fees. For CLOWNDOCTORS - encouraging more joy and happiness to children staying in hospitals (http://www.rednoses.eu/red-noses-organisations/slovenia/)

€2.00

Top SQL Server Bloggers 2018
TomazTsql

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Discover WordPress

A daily selection of the best content published on WordPress, collected for you by humans who love to read.

Revolutions

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

tenbulls.co.uk

tenbulls.co.uk - attaining enlightenment with the Microsoft Data and Cloud Platforms with a sprinkling of Open Source and supporting technologies!

SQL DBA with A Beard

He's a SQL DBA and he has a beard

Reeves Smith's SQL & BI Blog

A blog about SQL Server and the Microsoft Business Intelligence stack with some random Non-Microsoft tools thrown in for good measure.

SQL Server

for Application Developers

Business Analytics 3.0

Data Driven Business Models

SQL Database Engine Blog

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Search Msdn

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

R-bloggers

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

R-bloggers

R news and tutorials contributed by hundreds of R bloggers

Data Until I Die!

Data for Life :)

Paul Turley's SQL Server BI Blog

sharing my experiences with the Microsoft data platform, SQL Server BI, Data Modeling, SSAS Design, Power Pivot, Power BI, SSRS Advanced Design, Power BI, Dashboards & Visualization since 2009

Grant Fritchey

Intimidating Databases and Code

Madhivanan's SQL blog

A modern business theme

Alessandro Alpi's Blog

DevOps could be the disease you die with, but don’t die of.

Paul te Braak

Business Intelligence Blog

Sql Insane Asylum (A Blog by Pat Wright)

Information about SQL (PostgreSQL & SQL Server) from the Asylum.

Gareth's Blog

A blog about Life, SQL & Everything ...