Power BI R Script

Power BI R Script allows using R visuals or charts inside your dashboard. Using an R script inside the desktop is very helpful for the data scientists to place their work in one place.

In this section, we show you how to create a chart using Power BI R Script with an example. For this Script demonstration, we are going to use the Faithful CSV file created by Rstudio.

I suggest you refer to R Programming to understand the code that we used in this Power BI article.

Open Desktop and View Fields 1

Create a Power BI R Script Visual

In Power BI, you can use this Script Visual to create Charts inside a dashboard.

Click the R button within the visualization 2

First, click on the R Script Visual under the Visualization section. It automatically generates a Chart with a script editor, as shown in the below screenshot.

You have to use this script editor window to write the code.

View the Editor 3

Before you start writing code, you have to create a data set. It can be done by dragging the required fields to the Values section. For this demo, let me drag eruptions and wait for columns in the Values section.

Write your own code 4

As you can see from the above screenshot, Power BI has automatically generated a data set for you. Now, you have to use this data set to generate your visual.

Here, we used ggplot2 to generate 2D scatter plot. I suggest you refer to the Scatter Plot article to understand this code.

ggplot(dataset, aes(x = eruptions, y = waiting))  +  
     geom_point() + 

Once you have finished writing your script, click the Run button

Hit the Run button (little round circle) 5

From the screenshot below, you can see the Chart.

You can see the ggplot Scatter Plot 6

Like any other chart, you can apply filters on the chart as well. To demonstrate the Power BI R script filters, let me create a table first.

Add fields to Filters Section under Visualization 7

We created a column chart, as well.

Add Column Chart 8

I think there is too much data in the table to apply filters. So, let me create a cluster.

Choose Automatically find cluster option 9

We are leaving the default settings.

Change Number of Clusters 10

As you can see, it has created two clusters. Next, we replaced the Sno with the Cluster column in a Table.

Power BI R Script 11

Let me select Cluster 1. From the screenshot below, you can see that the visual is filtering.

Power BI R Script 12

Try with the other cluster too

Power BI R Script 13

Create a Power BI R Script Visual 2

In Power BI, you can also create an R visual with default datasets available. To demonstrate the same, we are using the Diamonds data set.

ggplot(diamonds, aes(x = carat, y = price, color = cut))  +  
       geom_point() + 
       geom_smooth(method = "auto", se = FALSE) +  
       scale_color_manual(values = c("orchid", "chocolate4",   
                                 "goldenrod2", "tomato2", "midnightblue"))

From the screenshot below, you can see the report and the code.

Power BI R Script 14