Power BI R Script allows to use R visuals or R charts inside your Power BI dashboard. This will be very helpful for the data scientists to place their work in one place. In this article we will show you, How to create a chart using Power BI R Script with example.
For this demonstration we are going to use the Faithful CSV file that was originally created for R studio. I suggest you to refer R Programming to understand the code that we used in this article.
Create a Power BI R Script Visual
In Power BI, you can use this R Script Visual to create R Charts inside a Power Bi dashboard.
First, click on the R Script Visual under the Visualization section. This will automatically create a R Chart with R script editor as shown in below screenshot.
You have to use this R script editor window to write R script.
Before you start writing R code, you have to create a data set. This can be done by dragging required fields to the Values section. For this demo, let me drag eruptions, and waiting column to Values section
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 create your R visual.
Here, we used ggplot2 to create 2D scatter plot. I suggest you to refer, Create a Scater Plot article to understand this code
library(ggplot2) ggplot(dataset, aes(x = eruptions, y = waiting)) + geom_point() + geom_density_2d()
Once you finished writing your script, click Run button
From the below screenshot, you can see the R Chart
Like any other chart in Power, you can apply filters on R chart as well. To demonstrate the same, let me create a table first.
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 the Cluster 1. From the below screenshot, you can see that the R visual is filtering.
Try with the other cluster too
Create a Power BI R Script Visual 2
In Power BI, you can also create a R visual with default data sets available in R. 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 below screenshot, you can see the report