How to generate custom visuals in Power BI using R | by Moumi Panja | Towards Data Science

Did you ever want to display your own custom visuals through Power BI? If the answer is yes, then this article will help you quickly understand how you can import custom visuals created in R and use them in your regular Power BI reports.
Installing R and R Studio: Assuming that Power BI desktop application is already installed, first you need to install the R language and an integrated development environment (IDE) to run R scripts. I am using R-3.5.1 and R Studio while writing this article.
Enabling R visuals in Power BI: From Power BI desktop, go to File > Options and settings > Options and then select your R home directory and IDE in the R Scripting tab.
Installing node.js: Visit this website and download the latest features installer. Follow the installation steps in the wizard to complete the process. Once done, restart the computer to begin.
Installing packages: Open the node.js command prompt and execute the following command:
npm install -g powerbi-visuals-tools
To check if the installation is successful, run the following command:
pbiviz 
You should see the help screen like the one shared below.
Data: I have used the classic Car Evaluation Data Set from UCI Machine Learning Repository. You can download it from here and save it in CSV format. According to this data set, the acceptability of a car for a customer depends on 6 main criteria — buying price, maintenance cost, number of doors, capacity in terms of persons to carry, luggage boot size and estimated safety of the car. Here’s the glimpse of the data set:
In this article, I will create an HTML 100% stacked column chart to show how the car acceptability varies depending on the buying price.
Connecting to the data: First, connect to your data from the Power BI desktop application using Get Data. Once the data is imported, click on the R script visual icon from the Visualization pane. Drag and drop the columns you want to use into the Values area. This will automatically generate the data set which you will work on. Click on the little side arrow at the corner of the R script editor to be redirected to R Studio so that you can try and test your code to generate custom visuals.
You can skip all the steps above and directly write the read.csv code in R Studio to connect to your data which is saved in your local machine.
Creating regular non-HTML visuals: Once the R Studio is opened, you can see that the code for reading your data set is already there and you can start your analysis immediately. Write down your R codes and run it into R studio to generate the visuals you want. I have used the popular ggplot2 library in R to visualize the relationship between buying price of a car with its acceptability.
library(ggplot2)g <- ggplot(data=dataset, aes(x=dataset$buying,                fill=dataset$car_acceptibility)) +            geom_bar(stat=”count”) +            scale_x_discrete(name =”Buying Price”) +           scale_fill_discrete(name=”Acceptability”)g
If you use the above piece of code directly in the R script editor in Power BI, it generates a chart which indicates that cars with very high and high buying price are likely to be unaccepted by majority of the customers. You can create your filters to slice and dice this interactive chart.
For example, if you want to see the number of accepted cars only in the various buying price category, you can create a slicer on car acceptability and filter down the data. Please note, this is not an HTML output.
Creating the rhtml template: Regular R-powered custom visual displays the output plot as a static image. To generate HTML visuals, you need to create the rhtml template. In the node.js command prompt, execute the following command:
pbiviz new demoCarEvaluation -t rhtml
This template includes a very basic ready to run R Visual that can create HTML output.
Using the rhtml template: Navigate to your local machine to find where demoCarEvaluation folder is created. In my example, clearly the folder is in C drive > Users > username. Once you locate the newly created folder, open it and edit the script.r file in R Studio. Paste your code which you wrote down to generate the static R visualization inside the ‘Actual Code’ section of script.r.
In the image below, you can see that I have pasted the entire code which I used previously to generate the stacked column chart above.
Running the rhtml template: Go to the node.js command prompt and navigate to the folder where the script.r is stored using the change directory command. To package the visual, execute the following command:
pbiviz package
Once the packaging is complete, return to the Power BI desktop application and click on Import From File in the Visualization pane. Browse to the dist folder inside the demoCarEvaluation and import the pbiviz file to use it in the report.
Creating the custom visual in Power BI: Once you complete all the steps above, you will get a new icon in the visualization pane. Click on the demoCarEvaluation icon, drag the columns you want to use in your visual under the Values field and you will see your HTML chart in the Report. Hover your mouse pointer on the chart, you will see further details on each of the part of the chart. There’s a range of activities supported in HTML visuals like zoom in/ out, autoscale etc.
If you want to create other visuals on the same data, you just need to edit the script.r file, run the pbiviz package command again and you will be able to create new visuals very quickly and easily.
Explore and enjoy!
<<Note: This is an old article originally published in March 2019. As Power BI changes rapidly, please check Microsoft’s documentation site for the latest updates.>>