R
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Data Structures
-
Section 4: Data Manipulation
-
Section 5: Visualization and Statistics
-
Section 6: Advanced R
-
Section 7: Practical Projects
-
Section 8: Interview Practice
-
Section 9: More Practice Exercises
-
Section 10: Shiny Apps in Depth
-
Section 11: More Data Wrangling
-
Section 12: Tidyverse Deep Dive
-
Section 13: Statistical Modeling Deep Dive
-
Section 14: Machine Learning in R
-
Section 15: R Visualization Deep Dive
-
Section 16: R Package Development Deep Dive
-
Section 17: R for Reproducible Research
-
Section 18: R and Databases
-
Section 19: R Performance Optimization
-
Section 20: Bioinformatics and Specialized R
-
Section 21: More Shiny Practice
-
Section 22: More Practice Exercises
-
Section 23: R Data Structures Deep Dive
-
Section 24: More Interview and Review
-
Section 25: R for Business Analytics
-
Section 26: R Text Mining and NLP
-
Section 27: R Spatial Data Analysis
-
Section 28: R Deep Learning
-
Section 29: Advanced Statistical Techniques
-
Section 30: R Object Systems Deep Dive
-
Section 31: R Environments and Metaprogramming
-
Section 32: R for Finance
-
Section 33: R for Clinical and Health Data
-
Section 34: More Shiny Advanced Practice
-
Section 35: R Data Cleaning Deep Dive
-
Section 36: R Reporting Automation
-
Section 37: More Practical Projects Round 2
-
Section 38: R Ecosystem and Career
-
Section 39: More Visualization Practice
-
Section 40: R for Experimentation
-
Section 41: R for Genomics and Bioinformatics Deep Dive
-
Section 42: R for Marketing Analytics
-
Section 43: R Data Import/Export Deep Dive
-
Section 44: R String Processing Deep Dive
-
Section 45: R for Actuarial and Insurance Analytics
-
Section 46: R Testing and Quality Assurance Deep Dive
-
Section 47: R Version Control and Collaboration
-
Section 48: R Advanced Functional Programming
-
Section 49: R for Supply Chain and Operations
-
Section 50: More Practice Exercises Round 3
-
Section 51: R Dashboards and BI Integration
-
Section 52: R Data Governance and Ethics
-
Section 53: More Modeling Practice
-
Section 54: R Final Capstone Projects
-
Section 55: R for Sports Analytics
-
Section 56: More Interview Practice Round 2
-
Section 57: R Networking and APIs
-
Section 58: R for Environmental Science
-
Section 59: R Notebook and Documentation Practices
-
Section 60: More Data Wrangling Mastery
-
Section 61: R for A/B Testing at Scale
-
Section 62: R Package Ecosystem Deep Dive
202: Connecting R to Power BI
Look, Power BI is an absolute beast when it comes to standard bar charts and slicers. But the second you want to do something a bit more "statistical"—like a correlation matrix or a complex forecast—you'll find that the built-in visuals start to feel restrictive. That's where we bring in R. Instead of fighting with Power Query's GUI, we can just drop a script right into the middle of our report.
For this lesson, let's imagine we have a dataset of store performance metrics: sales, foot traffic, marketing spend, and average customer spend. We want to see which of these variables actually move together. A correlation heatmap is the perfect tool for this, and it's significantly faster to write in R than to build manually in Power BI.
Feeding the R engine with Power BI data
First, I'm going to load my dataset into Power BI. Once the data is there, I'll click on the "R script visual" icon in the Visualizations pane. Power BI does something helpful here: it automatically creates a dataframe called dataset containing the columns I've dragged into the "Values" bucket. I'm dragging in Sales, Traffic, MarketingSpend, and AvgSpend.
Now, here is the logic I'm following: I need to calculate the correlation matrix first, and then pass that matrix into a visualization. I'm opting for ggplot2 because it gives me the most control over the aesthetics.
Sketching out the correlation heatmap
I'll jump into the R script editor and start typing. I want to melt the correlation matrix into a "long" format so ggplot2 can handle it. Here is the code I'm aiming for:
# Calculate correlation
cor_matrix <- cor(dataset)
# Convert to long format for ggplot
library(reshape2)
melted_cor <- melt(cor_matrix)
# Plot it
ggplot(melted_cor, aes(Var1, Var2, fill=value)) +
geom_tile() +
scale_fill_gradient2(low="blue", high="red", mid="white", midpoint=0)
Dealing with the "Function Not Found" headache
I hit "Run," and... nothing. Power BI throws a generic error saying it can't find the function ggplot. I stared at my code for a full minute before I realized I'd made the classic rookie mistake: I loaded reshape2, but I completely forgot to load ggplot2.
In a standard RStudio session, I probably have these libraries loaded in my global environment from an hour ago. But Power BI starts a fresh, isolated R session every time the visual refreshes. If it's not in the script, it doesn't exist. I'll add library(ggplot2) to the top and try again.
library(ggplot2)
library(reshape2)
cor_matrix <- cor(dataset)
melted_cor <- melt(cor_matrix)
ggplot(melted_cor, aes(Var1, Var2, fill=value)) +
geom_tile() +
scale_fill_gradient2(low="blue", high="red", mid="white", midpoint=0) +
labs(title="Metric Correlations", x="", y="")
Polishing the visual for the dashboard
Now that it's actually rendering, it looks a bit cramped. The axis labels are overlapping. To fix this, I'll add a quick theme adjustment. I'm adding theme_minimal() and rotating the x-axis text. It's a small tweak, but when this ends up on a corporate dashboard, those little details prevent the stakeholders from asking "What does this axis even say?"
The final result is a dynamic heatmap that updates automatically whenever the user filters the data using other Power BI slicers. We've essentially used Power BI as the data pipeline and R as the rendering engine.
📋 Practical Task
Building a Regional Order Distribution Boxplot
You have a dataset in Power BI containing Region (a category) and OrderValue (a numeric value). Your goal is to create an R visual that displays a boxplot of order values grouped by region to identify outliers.
Requirements:
- Use the
ggplot2library. - Map
Regionto the X-axis andOrderValueto the Y-axis. - Use
geom_boxplot()to create the visual. - Apply
theme_bw()to ensure the visual blends well with a professional Power BI report background. - Ensure you include all necessary
library()calls within the script to avoid execution errors.
There are no comments for now.