Loading and Saving
Data in R
Index
1. Loading Data in R
2. Saving Data in R
3. Integrating R with PowerBI
Loading Data from Built-in R
Datasets
R comes with many datasets inside the datasets package.
• You can load them using data().
• Example: Loading the mtcars Dataset
# Load built-in dataset
data(mtcars)
# Display first few rows
head(mtcars)
Loading Data from External R
Packages
Many R packages come with datasets. You can load them after
installing the package.
Example: Loading the iris Dataset from datasets Package
install.packages("datasets")
not available
library(datasets)
# Install if
# Load dataset
data(iris)
head(iris)
Example: Loading Data from ggplot2 Package
The ggplot2 package contains the diamonds dataset.
install.packages("ggplot2")
library(ggplot2)
# Load dataset
data(diamonds)
head(diamonds)
Saving Data in R
You can save data in various formats like CSV, RDS, and RData.
Saving as a CSV File
write.csv(mtcars, "mtcars_data.csv",
row.names=FALSE)
Saving as an RDS File (Recommended for R
use)
saveRDS(mtcars, "mtcars_data.rds")
Saving as an RData File (Multiple Objects)
save(mtcars, iris, file="datasets.RData")
Integrating R with Power
BI
1 . Using R in Power BI
•R Visuals: You can use R scripts to create custom visualizations in
Power BI.
•R Script Data Source: Import and transform data using R scripts in
Power Query.
•R in Power Query: Perform advanced data transformations and
statistical analysis before loading data into Power BI.
2. Why Use R in Power BI?
•Advanced Analytics: Use statistical models, machine learning, and
predictive analytics.
•Custom Visualizations: Create unique plots beyond built-in Power BI
visuals.
•Data Cleaning & Transformation: Handle complex ETL processes with R.
3. How to Integrate R in Power BI
•Install R Engine (e.g., R from CRAN or Microsoft R Open).
•Enable R scripting in Power BI Settings.
•Use the R script visual or R script data source in Power Query.
Enable R
visuals in
Power BI
Desktop
• From the Power BI Desktop menu,
select File > Options and
settings > Options.
• On the left side of
the Options page, under Global,
select R scripting.
• Under R script options, verify
that your local R installation is
specified in Detected R home
directories and that it properly
reflects the local R installation
you want Power BI Desktop to use.
In the following image, the path to
the local installation of R
is C:\Program Files\R Open\R-
Create R visuals in Power BI Desktop
1, Select the R Visual icon in
the Visualization pane to add an R
visual.
2. In the Enable script visuals window that appears,
select Enable.
Create R visuals in Power BI Desktop
When you add an R visual to a report, Power BI Desktop makes the following changes:
•A placeholder R visual image appears on the report canvas.
•The R script editor appears along the bottom of the center pane.
Create R visuals in Power BI Desktop
In the Values section of the Visualization pane, drag fields from the Fields pane that you want to
consume in your R script, just as you would with any other Power BI Desktop visual
Now you can use the data you selected to create a plot:
•As you select fields, the R script editor generates supporting R script binding code for those fields in
the gray section along the top of the editor pane.
•If you remove a field, the R script editor automatically removes the supporting code for that field.
Create R visuals in Power BI Desktop
A common example of using Power BI with R is to create a complex data visualization, like a customized
heatmap, where you leverage R's powerful statistical capabilities to analyze and present data within a Power
BI dashboard, allowing users to interact with the visualization based on different filters and selections.
Script in Power BI:
• Data Preparation: Use R code within Power BI to calculate specific metrics like
sales growth rate or market share for each product-region combination.
• Visualization Creation: Employ R packages like "ggplot2" to generate a heatmap
where:
• X-axis represents product categories
• Y-axis represents different regions
• Color intensity indicates the sales performance (higher sales = darker color)
Steps to create Heatmap using Power BI and R
Step 1: Load the Dataset into Power BI
1.Open Power BI Desktop.
2.Click on Home > Get Data > Text/CSV.
3.Select the sales_performance.csv file and load it into Power BI.
4.Click Load to bring the data into Power BI.
Step 2: Enable R in Power BI
1.Go to File > Options and Settings > Options.
2.Under R scripting, ensure you have an R engine installed (e.g., R from
CRAN or Microsoft R Open).
3.Set the correct R installation path.
Step 3: Add an R Script Visual
1.Click on "R script visual" from the Visualizations pane.
2.Drag the necessary fields into the Values section (e.g., Region, Month, Sales
Steps to create Heatmap using Power BI and R
Step 4: Write R Code for the Heatmap
In the R script editor, write the following R script:
# Load necessary libraries
library(ggplot2)
# Convert fields to factors for categorical data
dataset$Region <- as.factor(dataset$Region)
dataset$Month <- factor(dataset$Month, levels = c("Jan", "Feb", "Mar"))
# Ensure correct month order
# Create heatmap using ggplot
ggplot(dataset, aes(x = Month, y = Region, fill = `Sales ($)`)) +
geom_tile() +
scale_fill_gradient(low = "yellow", high = "red") +
labs(title = "Sales Performance Heatmap", x = "Month", y = "Region") +
theme_minimal()
Click Run to execute the script.
Steps to create Heatmap using Power BI and R
Step 5: Customize and Publish
• Adjust the colors or layout as needed.
• Add slicers for interactivity (e.g.,
by Product).
• Publish the report to the Power BI
Service if required.
Any Questions?
Thank You