Microsoft recently announced the ability to execute R scripts in Power BI to create visualizations. Power BI Desktop does not include the R engine. To run R scripts in Power BI Desktop, you must separately install R on your local computer. You can download R from here.
Being able to use the visualization capabilities from the R language is a huge improvement. In addition to all of this, you can use Power BI’s native slicing and cross-filtering capabilities to dynamically feed data into these visuals. Here, I am going to demonstrate how to enable R visuals in Power BI.
Open Power BI desktop application on your local PC which has R installed in it.
To enable R visuals, select File > Options and settings > Options
In the Options page that appears, navigate to the R Scripting on the left pane. Make sure your local R installation is specified in the R Scripting section and the path it displays properly reflects the local R installation you want Power BI Desktop to use. Click on OK.
In the Home menu, use Get Data to import the dataset you want to visualize using R.
Now that you have loaded the dataset, select the R Visual icon from the Visualization pane on the right, to add an R visual.
A placeholder for the R visual appears on the canvas along with the R script editor at the bottom of the screen as shown below.
Now, as the R script editor describes, now we need to drag the variable fields from the dataset into the Values area from the right hand side pane. In this example, we are going visualize the correlation between the different variables in the dataset. Only fields that have been added to the Values area will be available to your R script.
Note: You can add new variables or remove variables from the dataset to the Value area of your R visual any time.
After you added all the required variables to the Value area, you will see that the R script editor has automatically generated the following lines of code:
- A dataframe with all the variables you selected
- Remove duplicates/ Select unique rows
You can now leverage your R skills to modify the code in the R script editor.
Note: You will need to load all the R packages using the library function into your R script editor. Also, make sure these packages have already been pre-loaded/installed in your local computer using R studio.
Once done editing your code, click on RUN icon on the R script editor. As a result of your R script, an R visual will appear on the report canvas.
You can now add the slicers and other visuals you would like to see in your dashboard along with the R visual.
You have now learned how to use the R visuals in Power BI Desktop to visualize your dataset. You can now share them with your peers using the Power BI service. There is a lot more you can accomplish with this new cool Power BI feature. So stay tuned for more on this.