SlideShare a Scribd company logo
1 of 16
Download to read offline
Import-export Excel files
Using openxlsx
Rupak Roy
openxlsx()
ØOPENXLSX: Simplifies the creation of excel files by providing a high level
interface to read, write and format excel worksheets with the added benefit
of removing the dependency on Java.
Ø Import functions include:
loadWorkbook()
readWorkbook()
The features of XLConnect::readWorksheet() and
XLConnect::readWorkbookFromFile() are merged in the
openxlsx::readWorkbook()
Ø Export functions inlude:
createWorkbook()
addworksheet() – alternative to XLConnect::createSheet()
writedata() – alternative to XLConnectwriteWorkSheet()
saveWorkbook()
openxlsx::loadWorkbook()
 loadWorkbook(): Loads and returns a workbook object conserving styles
and format of the original .xlsx file
>loadWorkbook(file)
Where file = a path to an excel workbook to be loaded
#install the openxlsx package
>install.packages(“openxlsx”)
#load the functions from openxlsx package.
>library(openxlsx)
#load the excel workbook
> wb<- loadWorkbook("sample.xlsx")
>class(wb)
To know more about the features of loadWorkbook() use
>?openxlsx::loadWorkbook
openxlsx::readWorkbook()
 readWorkbook(): Reads the data from an Excel file or directly from a
workbook object of a loadWorkbook() function into a data.frame.
 >wb1<-readWorkbook(File, sheet = 1, startRow = 1, colNames = TRUE,
rowNames = FALSE, detectDates = FALSE, skipEmptyRows = TRUE,
skipEmptyCols = TRUE, check.names = FALSE, na.strings = "NA“, cols= 2:7,
rows = 5:10)
Where
file = An Excel file, Workbook object or URL to xlsx file.
sheet = The name or index of the sheet to read data from.
startRow = The index of the first column to read from. Empty rows at the top
of a file are always skipped, regardless of the value of startRow.
colNames = If TRUE, the first row of data will be used as column names.
rowNames = If TRUE, first column of data will be used as row names.
detectDates = If TRUE, attempt to recognise dates and perform conversion.
cols = A numeric vector specifying which range of columns in the Excel file to
read. If NULL, all columns are read. E.g. cols = c(1,5,7) gives the lists of 1,5 and
number 7 column and cols: 2:4 gives the range of columns from column 2 to 4.
The same goes with rows = 5:10 and rows =c(3,4,9)
openxlsx::readWorkbook()
#Read the 1st excel sheet from wb R object i.e. sample.xlsx file.
>rwb_store<-readWorkbook(wb,sheet = 1)
>View(rwb_store)
#Read the 2nd excel sheet directly from sample.xlsx file.
>rwb_bike<- readWorkbook (“sample.xlsx”, sheet=“bike_sharing_program”)
>View(rwb_bike)
#Optimized query for large datasets
>orwb_store<- readWorkbook("sample.xlsx",sheet = "store",colNames= T, cols
= 2:4, rows =14000:20000 )
To know more about the features of loadWorkbook() use
>?openxlsx::readWorkbook
Rupak Roy
openxlsx::addWorksheet()
 createWorkbook(): Creates a new workbook object.
>cwb<-createWorkbook()
 addWorksheet(): Adds a worksheet to a workbook object.
>addWorksheet(cwb, sheetName, header = NULL,orientation =
getOption("openxlsx.orientation", default = "portrait") )
Where as,
wb = a workbook object to attach the new worksheet
sheetname = a name for the new worksheet
orientation = One of "portrait" or "landscape“
Rupak Roy
openxlsx::addWorksheet()
#Create new empty excel/work sheets in a workbook object
>addWorksheet(cwb,"new_sheet1")
>addWorksheet(cwb,"new_sheet2")
>addWorksheet(cwb,"new_sheet3")
To know more about the features of addWorkSheet() we can always use
>?openxlsx::addWorksheet
Rupak Roy
openxlsx::writeData()
 writeData(): Writes an object to worksheet.
>writeData(wb, sheet, x, startCol = 1, startRow = 1, xy = NULL, colNames =
TRUE, rowNames = FALSE, headerStyle = NULL, keepNA = FALSE)
Where as,
wb = A Workbook object containing a worksheet.
sheetname = The worksheet to write to. Can be the worksheet index or name.
x = Object to be written e.g. object containing data.frame
startCol = A vector specifiying the starting column to write to.
startRow = A vector specifiying the starting row to write to.
xy = An alternative to specifying startCol and startRow individually. A vector of
the form c(startCol, startRow).
rowNames/colNames = If TRUE, data.frame row/col names of x are written.
keepNA = If TRUE, NA values are converted to #N/A in Excel else NA cells will be
empty.
Rupak Roy
openxlsx::writeData()
>wb<-loadWorkbook(“sample.xlsx”) #load a workbook
>names(wb) #list the available worksheets in the workbook
#Create an empty excel/worksheet in a workbook object
>addWorksheet(wb, “new_sheet”)
>names(wb)
#read a worksheet from the workbook (wb) and save it in a R object
>rwb<-readWorkbook(wb,sheet="store.")
#write the R object rwb (worksheet) in the workbook wb
>writeData(wb,sheet = "new_sheet",rwb)
#save the workbook in the disk
> saveWorkbook(wb,"my_first_workbook.xlsx")
To know more about the features of writeData() we can always use
>?openxlsx::writeData
openxlsx::saveWorkbook()
Few steps involved before we can use use openxlsx::saveWorkbook():
ü Install Rtools which is a collection of tools necessary for building R
packages in Windows.
Available for download at
https://cran.rproject.org/bin/windows/Rtools/
also included with this module.
ü Follow the installation guide in the next slide.
ü Set Sys.setenv("R_ZIPCMD" = “…………path/bin/zip.exe")
>Sys.setenv("R_ZIPCMD" = "C:/Rtools/bin/zip.exe")
 During the
installation
 Click ‘Next’ >
openxlsx::saveWorkbook()
 Select both
the checkboxes.
 Done.
openxlsx::saveWorkbook()
openxlsx::saveWorkbook()
 saveWorkbook(): save a workbook object to file
>saveWorkbook(wb, “file.xlsx”, overwrite= TRUE)
Where
wb = a workbook object to write to file
file = name of the file to save as
overwrite = If TRUE, overwrite any existing file
>saveWorkbook(wb,"my_first_workbook.xlsx")
To know more about the features of saveWorkbook() we can always use
>?openxlsx::writeData
Rupak Roy
readxl::read_excel()
Another common package used to read the excel .xlsx and .xls file with the
added benefit of removing the dependency on Java.
 read_excel(): Reads xls and xlsx files. read_excel() calls excel_format() to
determine if path is xls or xlsx, based on the file extension and the file itself,
in that order. Use read_xls() and read_xlsx() directly if you know better and
want to prevent such guessing.
>read_excel(path, sheet = NULL, range = NULL, col_names = TRUE, na = "", skip
= 0, n_max = Inf)
Where, path = path to the .xlsx or .xls file
sheet = Sheet to read either a name of a sheet or an integer (the position of the
sheet). If not mentioned by default reads the first sheet.
range = A cell range to read from, includes typical Excel ranges like "B3:D87"
n_max = Maximum number of data rows to read. Ignored if range is given
na = Defines missing value formats. Default it treats blank cells as missing data.
readxl::read_excel()
#install the readxl package
>install.packages(“readxl”)
#load the functions from readxl package.
>library(readxl)
#load the workbooksheet from the sample.xlsx workbook
>myworkbook<- read_excel(“sample.xlsx”, sheet= 2)
To know more about the features of read_excel() we can always use
>?readxl::read_excel
Rupak Roy
Next:
We will learn how to import data from popular
databases using RODBC package.
Import export Excel files
Rupak Roy

More Related Content

What's hot

Decision Tree - C4.5&CART
Decision Tree - C4.5&CARTDecision Tree - C4.5&CART
Decision Tree - C4.5&CARTXueping Peng
 
ML - Multiple Linear Regression
ML - Multiple Linear RegressionML - Multiple Linear Regression
ML - Multiple Linear RegressionAndrew Ferlitsch
 
Comparing EDA with classical and Bayesian analysis.pptx
Comparing EDA with classical and Bayesian analysis.pptxComparing EDA with classical and Bayesian analysis.pptx
Comparing EDA with classical and Bayesian analysis.pptxPremaGanesh1
 
Linear models for classification
Linear models for classificationLinear models for classification
Linear models for classificationSung Yub Kim
 
Natural Logs
Natural LogsNatural Logs
Natural Logsswartzje
 
Transformation of Random variables & noise concepts
Transformation of Random variables & noise concepts Transformation of Random variables & noise concepts
Transformation of Random variables & noise concepts Darshan Bhatt
 
The life changing magic of tidying up your data: The art and science of makin...
The life changing magic of tidying up your data: The art and science of makin...The life changing magic of tidying up your data: The art and science of makin...
The life changing magic of tidying up your data: The art and science of makin...MEASURE Evaluation
 
4.6 Exponential Growth and Decay
4.6 Exponential Growth and Decay4.6 Exponential Growth and Decay
4.6 Exponential Growth and Decaysmiller5
 
Monte Carlo Statistical Methods
Monte Carlo Statistical MethodsMonte Carlo Statistical Methods
Monte Carlo Statistical MethodsChristian Robert
 
Issues in Decision Tree by Ravindra Singh Kushwaha B.Tech(IT) 2017-21 Chaudha...
Issues in Decision Tree by Ravindra Singh Kushwaha B.Tech(IT) 2017-21 Chaudha...Issues in Decision Tree by Ravindra Singh Kushwaha B.Tech(IT) 2017-21 Chaudha...
Issues in Decision Tree by Ravindra Singh Kushwaha B.Tech(IT) 2017-21 Chaudha...RavindraSinghKushwah1
 

What's hot (20)

Decision Tree - C4.5&CART
Decision Tree - C4.5&CARTDecision Tree - C4.5&CART
Decision Tree - C4.5&CART
 
Model selection
Model selectionModel selection
Model selection
 
Getting Started with R
Getting Started with RGetting Started with R
Getting Started with R
 
ML - Multiple Linear Regression
ML - Multiple Linear RegressionML - Multiple Linear Regression
ML - Multiple Linear Regression
 
Comparing EDA with classical and Bayesian analysis.pptx
Comparing EDA with classical and Bayesian analysis.pptxComparing EDA with classical and Bayesian analysis.pptx
Comparing EDA with classical and Bayesian analysis.pptx
 
Kernel Method
Kernel MethodKernel Method
Kernel Method
 
Linear models for classification
Linear models for classificationLinear models for classification
Linear models for classification
 
Natural Logs
Natural LogsNatural Logs
Natural Logs
 
Poisson regression models for count data
Poisson regression models for count dataPoisson regression models for count data
Poisson regression models for count data
 
Binary Logistic Regression
Binary Logistic RegressionBinary Logistic Regression
Binary Logistic Regression
 
Transformation of Random variables & noise concepts
Transformation of Random variables & noise concepts Transformation of Random variables & noise concepts
Transformation of Random variables & noise concepts
 
Principal Component Analysis
Principal Component AnalysisPrincipal Component Analysis
Principal Component Analysis
 
Graph Theory: Trees
Graph Theory: TreesGraph Theory: Trees
Graph Theory: Trees
 
Cluster Analysis for Dummies
Cluster Analysis for DummiesCluster Analysis for Dummies
Cluster Analysis for Dummies
 
The life changing magic of tidying up your data: The art and science of makin...
The life changing magic of tidying up your data: The art and science of makin...The life changing magic of tidying up your data: The art and science of makin...
The life changing magic of tidying up your data: The art and science of makin...
 
4.6 Exponential Growth and Decay
4.6 Exponential Growth and Decay4.6 Exponential Growth and Decay
4.6 Exponential Growth and Decay
 
Classification
ClassificationClassification
Classification
 
Monte Carlo Statistical Methods
Monte Carlo Statistical MethodsMonte Carlo Statistical Methods
Monte Carlo Statistical Methods
 
Issues in Decision Tree by Ravindra Singh Kushwaha B.Tech(IT) 2017-21 Chaudha...
Issues in Decision Tree by Ravindra Singh Kushwaha B.Tech(IT) 2017-21 Chaudha...Issues in Decision Tree by Ravindra Singh Kushwaha B.Tech(IT) 2017-21 Chaudha...
Issues in Decision Tree by Ravindra Singh Kushwaha B.Tech(IT) 2017-21 Chaudha...
 
Decision Tree Learning
Decision Tree LearningDecision Tree Learning
Decision Tree Learning
 

Similar to Import and Export Excel Data using openxlsx in R Studio

Similar to Import and Export Excel Data using openxlsx in R Studio (20)

Import and Export Excel files using XLConnect in R Studio
Import and Export Excel files using XLConnect in R StudioImport and Export Excel files using XLConnect in R Studio
Import and Export Excel files using XLConnect in R Studio
 
Python openpyxl
Python openpyxlPython openpyxl
Python openpyxl
 
Relational data model
Relational data modelRelational data model
Relational data model
 
How to Read Excel Files in Java (1).pdf
How to Read Excel Files in Java (1).pdfHow to Read Excel Files in Java (1).pdf
How to Read Excel Files in Java (1).pdf
 
Files,blocks and functions in R
Files,blocks and functions in RFiles,blocks and functions in R
Files,blocks and functions in R
 
EPiServer report generation
EPiServer report generationEPiServer report generation
EPiServer report generation
 
ADVANCE ITT BY PRASAD
ADVANCE ITT BY PRASADADVANCE ITT BY PRASAD
ADVANCE ITT BY PRASAD
 
Xpath
XpathXpath
Xpath
 
09 ms excel
09 ms excel09 ms excel
09 ms excel
 
20130215 Reading data into R
20130215 Reading data into R20130215 Reading data into R
20130215 Reading data into R
 
Excel to SQL Server
Excel to SQL ServerExcel to SQL Server
Excel to SQL Server
 
Moving Data to and From R
Moving Data to and From RMoving Data to and From R
Moving Data to and From R
 
Lecture 04
Lecture 04Lecture 04
Lecture 04
 
Oracle sql loader utility
Oracle sql loader utilityOracle sql loader utility
Oracle sql loader utility
 
Sql loader good example
Sql loader good exampleSql loader good example
Sql loader good example
 
Statistics lab 1
Statistics lab 1Statistics lab 1
Statistics lab 1
 
Data structures
Data structures Data structures
Data structures
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
 
Apachepoitutorial
ApachepoitutorialApachepoitutorial
Apachepoitutorial
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 

More from Rupak Roy

Hierarchical Clustering - Text Mining/NLP
Hierarchical Clustering - Text Mining/NLPHierarchical Clustering - Text Mining/NLP
Hierarchical Clustering - Text Mining/NLPRupak Roy
 
Clustering K means and Hierarchical - NLP
Clustering K means and Hierarchical - NLPClustering K means and Hierarchical - NLP
Clustering K means and Hierarchical - NLPRupak Roy
 
Network Analysis - NLP
Network Analysis  - NLPNetwork Analysis  - NLP
Network Analysis - NLPRupak Roy
 
Topic Modeling - NLP
Topic Modeling - NLPTopic Modeling - NLP
Topic Modeling - NLPRupak Roy
 
Sentiment Analysis Practical Steps
Sentiment Analysis Practical StepsSentiment Analysis Practical Steps
Sentiment Analysis Practical StepsRupak Roy
 
NLP - Sentiment Analysis
NLP - Sentiment AnalysisNLP - Sentiment Analysis
NLP - Sentiment AnalysisRupak Roy
 
Text Mining using Regular Expressions
Text Mining using Regular ExpressionsText Mining using Regular Expressions
Text Mining using Regular ExpressionsRupak Roy
 
Introduction to Text Mining
Introduction to Text Mining Introduction to Text Mining
Introduction to Text Mining Rupak Roy
 
Apache Hbase Architecture
Apache Hbase ArchitectureApache Hbase Architecture
Apache Hbase ArchitectureRupak Roy
 
Introduction to Hbase
Introduction to Hbase Introduction to Hbase
Introduction to Hbase Rupak Roy
 
Apache Hive Table Partition and HQL
Apache Hive Table Partition and HQLApache Hive Table Partition and HQL
Apache Hive Table Partition and HQLRupak Roy
 
Installing Apache Hive, internal and external table, import-export
Installing Apache Hive, internal and external table, import-export Installing Apache Hive, internal and external table, import-export
Installing Apache Hive, internal and external table, import-export Rupak Roy
 
Introductive to Hive
Introductive to Hive Introductive to Hive
Introductive to Hive Rupak Roy
 
Scoop Job, import and export to RDBMS
Scoop Job, import and export to RDBMSScoop Job, import and export to RDBMS
Scoop Job, import and export to RDBMSRupak Roy
 
Apache Scoop - Import with Append mode and Last Modified mode
Apache Scoop - Import with Append mode and Last Modified mode Apache Scoop - Import with Append mode and Last Modified mode
Apache Scoop - Import with Append mode and Last Modified mode Rupak Roy
 
Introduction to scoop and its functions
Introduction to scoop and its functionsIntroduction to scoop and its functions
Introduction to scoop and its functionsRupak Roy
 
Introduction to Flume
Introduction to FlumeIntroduction to Flume
Introduction to FlumeRupak Roy
 
Apache Pig Relational Operators - II
Apache Pig Relational Operators - II Apache Pig Relational Operators - II
Apache Pig Relational Operators - II Rupak Roy
 
Passing Parameters using File and Command Line
Passing Parameters using File and Command LinePassing Parameters using File and Command Line
Passing Parameters using File and Command LineRupak Roy
 
Apache PIG Relational Operations
Apache PIG Relational Operations Apache PIG Relational Operations
Apache PIG Relational Operations Rupak Roy
 

More from Rupak Roy (20)

Hierarchical Clustering - Text Mining/NLP
Hierarchical Clustering - Text Mining/NLPHierarchical Clustering - Text Mining/NLP
Hierarchical Clustering - Text Mining/NLP
 
Clustering K means and Hierarchical - NLP
Clustering K means and Hierarchical - NLPClustering K means and Hierarchical - NLP
Clustering K means and Hierarchical - NLP
 
Network Analysis - NLP
Network Analysis  - NLPNetwork Analysis  - NLP
Network Analysis - NLP
 
Topic Modeling - NLP
Topic Modeling - NLPTopic Modeling - NLP
Topic Modeling - NLP
 
Sentiment Analysis Practical Steps
Sentiment Analysis Practical StepsSentiment Analysis Practical Steps
Sentiment Analysis Practical Steps
 
NLP - Sentiment Analysis
NLP - Sentiment AnalysisNLP - Sentiment Analysis
NLP - Sentiment Analysis
 
Text Mining using Regular Expressions
Text Mining using Regular ExpressionsText Mining using Regular Expressions
Text Mining using Regular Expressions
 
Introduction to Text Mining
Introduction to Text Mining Introduction to Text Mining
Introduction to Text Mining
 
Apache Hbase Architecture
Apache Hbase ArchitectureApache Hbase Architecture
Apache Hbase Architecture
 
Introduction to Hbase
Introduction to Hbase Introduction to Hbase
Introduction to Hbase
 
Apache Hive Table Partition and HQL
Apache Hive Table Partition and HQLApache Hive Table Partition and HQL
Apache Hive Table Partition and HQL
 
Installing Apache Hive, internal and external table, import-export
Installing Apache Hive, internal and external table, import-export Installing Apache Hive, internal and external table, import-export
Installing Apache Hive, internal and external table, import-export
 
Introductive to Hive
Introductive to Hive Introductive to Hive
Introductive to Hive
 
Scoop Job, import and export to RDBMS
Scoop Job, import and export to RDBMSScoop Job, import and export to RDBMS
Scoop Job, import and export to RDBMS
 
Apache Scoop - Import with Append mode and Last Modified mode
Apache Scoop - Import with Append mode and Last Modified mode Apache Scoop - Import with Append mode and Last Modified mode
Apache Scoop - Import with Append mode and Last Modified mode
 
Introduction to scoop and its functions
Introduction to scoop and its functionsIntroduction to scoop and its functions
Introduction to scoop and its functions
 
Introduction to Flume
Introduction to FlumeIntroduction to Flume
Introduction to Flume
 
Apache Pig Relational Operators - II
Apache Pig Relational Operators - II Apache Pig Relational Operators - II
Apache Pig Relational Operators - II
 
Passing Parameters using File and Command Line
Passing Parameters using File and Command LinePassing Parameters using File and Command Line
Passing Parameters using File and Command Line
 
Apache PIG Relational Operations
Apache PIG Relational Operations Apache PIG Relational Operations
Apache PIG Relational Operations
 

Recently uploaded

Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 

Recently uploaded (20)

Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 

Import and Export Excel Data using openxlsx in R Studio

  • 1. Import-export Excel files Using openxlsx Rupak Roy
  • 2. openxlsx() ØOPENXLSX: Simplifies the creation of excel files by providing a high level interface to read, write and format excel worksheets with the added benefit of removing the dependency on Java. Ø Import functions include: loadWorkbook() readWorkbook() The features of XLConnect::readWorksheet() and XLConnect::readWorkbookFromFile() are merged in the openxlsx::readWorkbook() Ø Export functions inlude: createWorkbook() addworksheet() – alternative to XLConnect::createSheet() writedata() – alternative to XLConnectwriteWorkSheet() saveWorkbook()
  • 3. openxlsx::loadWorkbook()  loadWorkbook(): Loads and returns a workbook object conserving styles and format of the original .xlsx file >loadWorkbook(file) Where file = a path to an excel workbook to be loaded #install the openxlsx package >install.packages(“openxlsx”) #load the functions from openxlsx package. >library(openxlsx) #load the excel workbook > wb<- loadWorkbook("sample.xlsx") >class(wb) To know more about the features of loadWorkbook() use >?openxlsx::loadWorkbook
  • 4. openxlsx::readWorkbook()  readWorkbook(): Reads the data from an Excel file or directly from a workbook object of a loadWorkbook() function into a data.frame.  >wb1<-readWorkbook(File, sheet = 1, startRow = 1, colNames = TRUE, rowNames = FALSE, detectDates = FALSE, skipEmptyRows = TRUE, skipEmptyCols = TRUE, check.names = FALSE, na.strings = "NA“, cols= 2:7, rows = 5:10) Where file = An Excel file, Workbook object or URL to xlsx file. sheet = The name or index of the sheet to read data from. startRow = The index of the first column to read from. Empty rows at the top of a file are always skipped, regardless of the value of startRow. colNames = If TRUE, the first row of data will be used as column names. rowNames = If TRUE, first column of data will be used as row names. detectDates = If TRUE, attempt to recognise dates and perform conversion. cols = A numeric vector specifying which range of columns in the Excel file to read. If NULL, all columns are read. E.g. cols = c(1,5,7) gives the lists of 1,5 and number 7 column and cols: 2:4 gives the range of columns from column 2 to 4. The same goes with rows = 5:10 and rows =c(3,4,9)
  • 5. openxlsx::readWorkbook() #Read the 1st excel sheet from wb R object i.e. sample.xlsx file. >rwb_store<-readWorkbook(wb,sheet = 1) >View(rwb_store) #Read the 2nd excel sheet directly from sample.xlsx file. >rwb_bike<- readWorkbook (“sample.xlsx”, sheet=“bike_sharing_program”) >View(rwb_bike) #Optimized query for large datasets >orwb_store<- readWorkbook("sample.xlsx",sheet = "store",colNames= T, cols = 2:4, rows =14000:20000 ) To know more about the features of loadWorkbook() use >?openxlsx::readWorkbook Rupak Roy
  • 6. openxlsx::addWorksheet()  createWorkbook(): Creates a new workbook object. >cwb<-createWorkbook()  addWorksheet(): Adds a worksheet to a workbook object. >addWorksheet(cwb, sheetName, header = NULL,orientation = getOption("openxlsx.orientation", default = "portrait") ) Where as, wb = a workbook object to attach the new worksheet sheetname = a name for the new worksheet orientation = One of "portrait" or "landscape“ Rupak Roy
  • 7. openxlsx::addWorksheet() #Create new empty excel/work sheets in a workbook object >addWorksheet(cwb,"new_sheet1") >addWorksheet(cwb,"new_sheet2") >addWorksheet(cwb,"new_sheet3") To know more about the features of addWorkSheet() we can always use >?openxlsx::addWorksheet Rupak Roy
  • 8. openxlsx::writeData()  writeData(): Writes an object to worksheet. >writeData(wb, sheet, x, startCol = 1, startRow = 1, xy = NULL, colNames = TRUE, rowNames = FALSE, headerStyle = NULL, keepNA = FALSE) Where as, wb = A Workbook object containing a worksheet. sheetname = The worksheet to write to. Can be the worksheet index or name. x = Object to be written e.g. object containing data.frame startCol = A vector specifiying the starting column to write to. startRow = A vector specifiying the starting row to write to. xy = An alternative to specifying startCol and startRow individually. A vector of the form c(startCol, startRow). rowNames/colNames = If TRUE, data.frame row/col names of x are written. keepNA = If TRUE, NA values are converted to #N/A in Excel else NA cells will be empty. Rupak Roy
  • 9. openxlsx::writeData() >wb<-loadWorkbook(“sample.xlsx”) #load a workbook >names(wb) #list the available worksheets in the workbook #Create an empty excel/worksheet in a workbook object >addWorksheet(wb, “new_sheet”) >names(wb) #read a worksheet from the workbook (wb) and save it in a R object >rwb<-readWorkbook(wb,sheet="store.") #write the R object rwb (worksheet) in the workbook wb >writeData(wb,sheet = "new_sheet",rwb) #save the workbook in the disk > saveWorkbook(wb,"my_first_workbook.xlsx") To know more about the features of writeData() we can always use >?openxlsx::writeData
  • 10. openxlsx::saveWorkbook() Few steps involved before we can use use openxlsx::saveWorkbook(): ü Install Rtools which is a collection of tools necessary for building R packages in Windows. Available for download at https://cran.rproject.org/bin/windows/Rtools/ also included with this module. ü Follow the installation guide in the next slide. ü Set Sys.setenv("R_ZIPCMD" = “…………path/bin/zip.exe") >Sys.setenv("R_ZIPCMD" = "C:/Rtools/bin/zip.exe")
  • 11.  During the installation  Click ‘Next’ > openxlsx::saveWorkbook()
  • 12.  Select both the checkboxes.  Done. openxlsx::saveWorkbook()
  • 13. openxlsx::saveWorkbook()  saveWorkbook(): save a workbook object to file >saveWorkbook(wb, “file.xlsx”, overwrite= TRUE) Where wb = a workbook object to write to file file = name of the file to save as overwrite = If TRUE, overwrite any existing file >saveWorkbook(wb,"my_first_workbook.xlsx") To know more about the features of saveWorkbook() we can always use >?openxlsx::writeData Rupak Roy
  • 14. readxl::read_excel() Another common package used to read the excel .xlsx and .xls file with the added benefit of removing the dependency on Java.  read_excel(): Reads xls and xlsx files. read_excel() calls excel_format() to determine if path is xls or xlsx, based on the file extension and the file itself, in that order. Use read_xls() and read_xlsx() directly if you know better and want to prevent such guessing. >read_excel(path, sheet = NULL, range = NULL, col_names = TRUE, na = "", skip = 0, n_max = Inf) Where, path = path to the .xlsx or .xls file sheet = Sheet to read either a name of a sheet or an integer (the position of the sheet). If not mentioned by default reads the first sheet. range = A cell range to read from, includes typical Excel ranges like "B3:D87" n_max = Maximum number of data rows to read. Ignored if range is given na = Defines missing value formats. Default it treats blank cells as missing data.
  • 15. readxl::read_excel() #install the readxl package >install.packages(“readxl”) #load the functions from readxl package. >library(readxl) #load the workbooksheet from the sample.xlsx workbook >myworkbook<- read_excel(“sample.xlsx”, sheet= 2) To know more about the features of read_excel() we can always use >?readxl::read_excel Rupak Roy
  • 16. Next: We will learn how to import data from popular databases using RODBC package. Import export Excel files Rupak Roy