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



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 :


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 ( in GitHub.


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

Then in command Prompt write the below codes to install it


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


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



I check the folder that this packages has been create


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.


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)



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




Leila Etaati on LinkedinLeila Etaati on TwitterLeila Etaati on Youtube
Leila Etaati
Trainer, Consultant, Mentor
Leila is the first Microsoft AI MVP in New Zealand and Australia, She has Ph.D. in Information System from the University Of Auckland. She is the Co-director and data scientist in RADACAD Company with more than 100 clients in around the world. She is the co-organizer of Microsoft Business Intelligence and Power BI Use group (meetup) in Auckland with more than 1200 members, She is the co-organizer of three main conferences in Auckland: SQL Saturday Auckland (2015 till now) with more than 400 registrations, Difinity (2017 till now) with more than 200 registrations and Global AI Bootcamp 2018. She is a Data Scientist, BI Consultant, Trainer, and Speaker. She is a well-known International Speakers to many conferences such as Microsoft ignite, SQL pass, Data Platform Summit, SQL Saturday, Power BI world Tour and so forth in Europe, USA, Asia, Australia, and New Zealand. She has over ten years’ experience working with databases and software systems. She was involved in many large-scale projects for big-sized companies. She also AI and Data Platform Microsoft MVP. Leila is an active Technical Microsoft AI blogger for RADACAD.


  • 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?

  • Indeed this is exciting. What is not clear to me is why can’t this process be automated? Why can’t we simply drop a piece of R code (with the R script visual alreay in PBI) which might or might not contain interactivity and have PBI automatically and internally handle both cases, just like RStudio does.

    • Hi Den,
      Thanks, actually, it is possible to drop R code for R visual and have one, this will help to share the code and also for reducing the possibility of changing code by others, also another important thing is that we don’t able to use plotly in normal R scripts editor in Power BI report but with this approach we able to use this package.

Leave a Reply

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