Running an R Visualization in Power BI Desktop

Leave a comment

In the previous posts, we installed R for Windows and RStudio as an IDE for R. Both are configured to work with Power BI. In this post, we will run an R Visualization in Power BI.

In Power BI Desktop, you will see the R visualization type under Visualizations:

Selecting it, we see the message to Enable script visuals. Note the warning and click Enable if you want to proceed:

Now we can add R code. We can see in the bottom of the canvas is an R script editor pane:

In our dataset, we have 2 fields – Account Name and Revenue. Add those to the fields of the visual:

The following code has now automatically been generated:

# The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script:
# The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script: 
# dataset <- data.frame(Account Name, Revenue)
# dataset <- unique(dataset)
# Paste or type your script code here:

Let’s add our code after this, to display a bar chart of Account Name vs Revenue:

# The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script:
# dataset <- data.frame(Account Name, Revenue)
# dataset <- unique(dataset)
library(DT)
datatable(dataset)
plot(datatable)

In the R Script Editor, click the Play button:

This will display our R Visualization. We can see it is similar to our out of the box Power BI column chart:

Note you can also run the code inside your IDE, in my case RStudio, by clicking on the arrow in Power BI:

Which opens the script in RStudio. Press Run to run the script and display the chart:

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

Leave a Reply

Your email address will not be published. Required fields are marked *