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/
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.
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
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
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
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.
Solved it; got to do with sourcing the utility functions used for building .hmtl file.
Hi Ivan,
Sorry I got busy yesterday a bit, it seems you find solution
Thanks for following my work
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?
Thanks! yes for custom visual we have to change the original code and run the package again
Hi John,
I should admit now I able to do that separate x,y, the legend by changing the capabilities.json file, will write a blog post on that.
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.
I think this can be done soon
Hi Leila,
Can you please share the toy script you demonstrated in this blog.
Thanks in advance
Lalit Chaudhari
Hi, i will do, let me check with web site manager
Hi Lalit, there is a technical problem for sharing files, I will put in GitHub soon and will announce in blog