How to generate custom visuals in Power BI using R

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.

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


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.

Enabling R home directory and IDE in Power BIInstalling 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-toolsTo check if the installation is successful, run the following command: pbiviz and you should see the help screen like the one shared below.

Successful installation of Power BI visual toolsData: 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:Car evaluation data setIn this article, I will create an HTML 100% stacked column chart to show how the car acceptability varies depending on the buying price.

Creating HTML visuals using R: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.

Step by step instruction on how to create R script visualsYou 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.

Buying price vs.

Car acceptabilitylibrary(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”)gIf 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.

Filtered chart to display only accepted carsFor example, if you want 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 rhtmlrhtml template creationThis 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.


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 packageChanging the directoryCreating the packageOnce 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.

HTML custom visual using RExplore and enjoy!.

. More details

Leave a Reply