Power BI R Script allows using R visuals or charts inside your dashboard. Using an R script inside the Power BI desktop is very helpful for 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 will use the Faithful CSV file created by Rstudio.
I suggest you refer to R Programming to understand the code used in this Power BI article.
Create a Power BI R Script Visual
On a Desktop, you can use this Script Visual to create Charts inside a dashboard.
First, click on the R Script Visual under the Power BI 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.
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. Let me drag eruptions and wait for columns in the Values section for this demo.
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 a 2D scatter plot. I suggest you refer to the Scatter Plot article to understand this code.
library(ggplot2) ggplot(dataset, aes(x = eruptions, y = waiting)) + geom_point() + geom_density_2d()
Once you have finished writing your script, click the Run button
From the screenshot below, you can see the Chart.
Like any other chart, you can also apply filters on the chart. First, let me create a table to demonstrate the Power BI R script filters.
We created a column chart, as well.
I think there is too much data in the table to apply filters. So, let me create a cluster.
We are leaving the default settings.
As you can see, it has created two clusters. Next, we replaced the Sno with the Cluster column in a Table.
Let me select Cluster 1. From the screenshot below, you can see that the visual is filtering.
Try with the other cluster too
Create a Power BI R Script Visual 2
You can also create an R visual on the desktop with default datasets available. To demonstrate the same, we are using the Diamonds data set.
library(ggplot2) 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.