Interactive Charts using R and Power BI: Create Custom Visual Part 1

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

13

I am so excited about using Plotly packages in Power BI.

So What is Plotly: is an R package for creating interactive web-based graphs via the open source JavaScript graphing library :https://plot.ly/r/getting-started/

20170612_131532

this feature has been added recently and had been announce by Christian Christian Berg in Data insight summit 2017.

I started to search about it and I found an article by Avi Sander (https://github.com/Microsoft/PowerBI-visuals/blob/master/RVisualTutorial/CreateRHTML.md) in GitHub.

6

from the article I understand that I should create a custom Visual with R packages but How?. I asked My dear friends Rudiger Hein about it, and He sent me a nice video created by  Stephanie Locke and Now I am going to have some series of posts on how to have more nice charts using Plotly in Power BI!

To have Plotly inside Power BI you should create a custom visual from your R codes.

in this post I am going to show how to set up and create custom R visual inside Power BI, and in the next posts I will show some other nice charts!

1-first Step

first you should download NodeJS 4.0+ Required (5.0 recommended)  from https://nodejs.org/

Then in command Prompt write the below codes to install it

1

you should see above message after writing “npm install -g powerbi-visuals-tools” in command prompt.

To confirm it was installed correctly you can run the command without any parameters which should display the help screen.

then you should see the information about Power BI Custom Visual Tools

3

Yes, now you have the pbiviz in your machine

2-Second Step

Now I am going to follow the steps proposed by Avi Sander. we are going to create a “rhtml” template.

So in command prompt please type

Then now you should see that a new custom visual is installing and a new package has been created, but what is inside the packages

 

4

I check the folder that this packages has been create

5

this is a folder that provides a templatefor me to create other R custom visuals, please check the file “script.r” inside the folder.

Now as you can see in above code, we need library “Plotly and ggplot2 to draw a simple ggplot2 chart with interactive of Plotly.

the data set has been hard coded here for “iris” that is an open source dataset in R. the plot get the data from iris data set and shows the Petal.Lengh and the Species there. then we use the ggplotly function to show the data.

so we have a r scripts, now I am going to create a package from this first, and then I will write my own codes to create different charts.

so I back to command prompt and I type  just “pbiviz package” in the folder.

8

now I open a new Power BI project and as youcan see in the below video, you able to work with it now!

So exciting!

3- Third Step

Now I am going to change the code to have my own R scripts that able to work with the code

I just change the code a bit as below:

as you can see I change the data field as value, and also thefill with Type, so in any code if I have a Data and type column (can be created via custom column in Power bi) Icna show the below codes

so I just save the r script file, then I run the package again via

“pbiviz package” in command prompt, then I add the chart to power bi and now I have the chart in my power bi visual!

after importing the custom visual, now I am going to show the gender and age of people from my insurance data.

so I have to create a custom column with the name Data for age of people and another custom column with name Type for people age. as you can see in the below picture I created a column name Type that I put the gender for it, then I created another custom column for Age of people and I name it Data (in next picture)

13

12

Now I have a chart that it is interactive and you able to see the details (see below video)

 

 

in next posts I will show some exciting chart using Plotly in Power BI

References:

https://www.npmjs.com/package/powerbi-visuals-tools

https://github.com/Microsoft/PowerBI-visuals/blob/master/RVisualTutorial/CreateRHTML.md

 

 

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail
rssyoutuberssyoutube
Leila Etaati
Dr. Leila Etaati is Principal Data Scientist, BI Consultant, and Speaker. She has over 10 years’ experience working with databases and software systems. She was involved in many large-scale projects for big sized companies. Leila has PhD of Information System department, University of Auckland, MS and BS in computer science. Leila is Microsoft Data Platform MVP.

5 Comments

  • Hi Lelia,
    Great walkthrough as usual! I followed the steps as well as read the Github post, but it’s not working in Power BI desktop. I tested the script.R generated in Rstudio and it’s working; this is using the base R ‘iris’ data.

    As I’m already building stuff with plotly, it’d be awesome to tie this to PBI.

    If you may, we can exchange the .pbiviz generated for this toy problem and see the difference, if any.

    Look FW to you insights.

  • Hi Leila,

    I followed along well to your code good job.

    At the Power BI end, I believe the values are hard coded in. So if you wanted to switch from Value on the x axis, you would have to change the r script in the pbiviz folder. Rather than drag and drop in Power BI.

    Is this correct?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">