Working with R Scripts in Power BI Desktop

Working with R scripts in Power BI Desktop allows you to leverage the powerful capabilities of the R programming language for data analysis, visualization, and advanced analytics within your Power BI reports. Here’s how you can work with R scripts in Power BI Desktop:

Let’s see the step-by-step instructions on how to work with R Scripts in Power BI Desktop:

Install R on your Desktop

From the CRAN website, you can download the R Studio application onto your desktop. Run the installer and follow the instructions to install R on your desktop.

After installing the application ensure that R is correctly installed by running the following command in your command Prompt:

 >> R --version

Import Data Using R Script

First step in data analysis using power BI we need to import data. Power BI Allows many ways to import the data. Here we use R scripts to import data onto power BI.

Step 1: In Power BI Desktop, go to Home > Get Data > More….

Power BI User Interface

Step 2: Select Other > R scripting . We do this to ensure that R Script installed in our computer has been dedected by the power BI.

Options menu

Step 3: Under the R Scripting window, you can see that the power BI Automatically detects the directory where the R Studio is installed. If it is found unable to dedect the location, you have to manualy browse that.

R Script Option to detect the R Studio Availability

Get Data Menu

Step 4: From the Get Data tab, choose More option . Further in the Get data Window, click Other to find the R Script option.

Getting the Data using R Script

Step 5: After selecting the R Script Option , click connect option to establish the connection between Power BI and R studios

R Script to import Random Data

Step 6: A new R script terminal opens, where you can enter the R program to import the data. Here for the sake of example, we have coded to generate random data using the seed functionality. Then we convert the random generated data to a Data Frame.

Data Visalization in Power BI using R Scripts

Power BI is an interesting tool when it comes to visualization of our data. It has a simple user interface that allows visulization of the data seemingly interesting. We only require to drag and drop our column from our dataset onto the visualizaiton panel to generate attractive graphical representation of our data.

Visualization Panel to Visualize the loaded Data

When it comes to using R scripting for the visualization , click the R button under the visualization panel. You will see that a R script editor window opens as shown in the picture above. As its placeholder states, you are required to drag the fields into the values area in the visalization pane to start scripting. You can just tick the checkbox of the required columns for visualization.

Create Visualizations

After choosing the columns for visualization, lets script the visalization code using R Programming. We create a scatter plot for our random data to see how spread our data is. We make use of the popular library ggplot for the above purpose.

R
# Generate random data
set.seed(123)  

# Create a dataframe with random values
n <- 100  
dataset <- data.frame(
  ID = 1:n,
  Category = sample(letters[1:5], n, replace = TRUE),
  Value = rnorm(n, mean = 50, sd = 10)
)

library(ggplot2)

# Scatter plot
ggplot(dataset, aes(x = ID, y = Value, color = Category)) +
  geom_point() +
  labs(x = "ID", y = "Value") +
  ggtitle("Scatter Plot of Random Data")

Output:

R Script to generate a Scatter Plot.

After completing the code , click on the run button to view our scatter plot that we have created. Seems interesting right!!

Yes . Power BI is powerful tool when it comes to generating unique and attractive dashboards withing seconds.

Generated Scatter plot for our random dataset

Working with R Scripts in Power BI Desktop

Power BI is a very useful tool when it comes to working with powerful scripting languages such as R Programming. Power BI in itself is a powerful tool for data visualization. Using R scripts in Power BI Desktop can further enhance your data analysis and visualization capabilities, making the job of data analysts much easier.

Similar Reads

Working with R Scripts in Power BI Desktop

Working with R scripts in Power BI Desktop allows you to leverage the powerful capabilities of the R programming language for data analysis, visualization, and advanced analytics within your Power BI reports. Here’s how you can work with R scripts in Power BI Desktop:...

Conclusion

In this article we have seen how to use R Scripting in power BI efficiently . Dont limit yourself in just creating boring visualizations and dashboards. Go for transforming your datasets, merging them with other datasets and some unique plots to visualze your data efficiently. To learn more about R programming and its variety of tools available for data analysis, do check out this link....