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

 

1-first Step

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

That is the first Step.

Then in command Prompt write the below codes to install the Power BI visual tools o your PC

you need to be admin of your PC so able to install.

npm install -g powerbi-visuals-tools

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.

pbiviz

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 an “rhtml” template. IN this stage, we are going to download a sample visual name sampleRHTMLRvisual.

first, you need to create a new folder, then in command prompt change the new folder.

 

So in command prompt please type

pbiviz new sampleRHTMLVisual -t rhtml

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

 

Just check the RcustomVis folder you should see below picture

 

 

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

source('./r_files/flatten_HTML.r')

############### Library Declarations ###############
libraryRequireInstall("ggplot2");
libraryRequireInstall("plotly")
####################################################

################### Actual code ####################
g = qplot(`Petal.Length`, data = iris, fill = `Species`, main = Sys.time());
####################################################

############# Create and save widget ###############
p = ggplotly(g);
internalSaveWidget(p, 'out.html');
####################################################

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

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

so we have an r script, 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.

What is New in 2019

But as you can see in the picture above, you will get an error that you need to provide the name, description and so forth…

so I need to go to the folder–>pbiviz.json and open it in Notebook or Notepad

Now back to command prompt and run it again ( in this context, run mean write pbiviz package)

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

So exciting!

 

 

 

 

in the 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

 

 

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.

12 thoughts on “Interactive Charts using R and Power BI: Create Custom Visual Part 1

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

  • Hi Leila,
    Can you please share the toy script you demonstrated in this blog.
    Thanks in advance
    Lalit Chaudhari

Leave a Reply