Ability to add R visuals in the reports gives that extra edge to Power BI as a visualization tool. R has large number of packages for statistical data visualization and is a leading tool for statistical data plots.
While it is good to have understanding of R script, it is not mandatory to build charts from R engine in Power BI. Below I have given step bt step details of how to build a chart in Power BI using R engine.
Step-1: Install Power BI desktop and R engine
Power BI desktop does not comes with R engine, these are two different tools. Power BI desktop can be installed from below link:
R can be installed from many sources, but below are two primary official sources from which you can download.
- CRAN Repository: Open source CRAN repository of R
- MRAN Repository: Microsoft R open version which is an enhanced distribution of open R
Step-2: Link R Scripting in Power BI desktop
If you have already installed MRAN before installing Power BI desktop, it will add the path at the time of installation of Power BI desktop. Otherwise, these two need to be linked.
Link Power BI and R
Select File > Options and settings > Options and in the Options page that appears, make sure your local R installation is specified in the R Scripting section of the Options window, as shown in the following image.
Step-3: Enable Script Visuals in Power BI
First time when you try adding R visuals in Power BI, it will ask to enable visuals from scripting. Click Enable.
This will add R scripting pane and R visual in the Power BI desktop canvas. Your screen will look as below.
Step-4: Import sample data
I have used sample data available in CSV format. Refer to this link to download the data sample:
Import this data into PowerBI desktop. Your screen should look as in the following images.
Power BI Sample data
Step-5: Run your first R visual
Rename Column1 to "JUDGENAME". Select few fields by clickig checkboxes, I have selected CFMG, CONT, DECI, DILG fields. Then in the R script editor type the code and run the script.plot(dataset)
There is your first R visual from Power BI Desktop, take a snap!
R visuals in Power BI
Step-6: Customizing R visuals in Power BI desktop
R offers range of libraries to visualize the data. Most of that can be exploited to render rich visuals in Power BI. Below link from Microsoft has details which R packages can be used in Power BI:
I have used ggplot1 and reshape2 packages to make the visuals little more interesting. These visuals can be lot more customized, for that you should refer to individual R packages. For example, try running below script in your R script pane.library("ggplot2")
ds <- melt(dataset, id.vars = "JUDGENAME")
p <- ggplot(data=ds, mapping= aes(x=JUDGENAME,y=value,fill=variable))
p <- p+geom_bar(stat="identity")
You should get output like below.
Stacked Bar Chart in Power BI R Visual