SlideShare a Scribd company logo
Import export Excel files
Using XLConnect
Rupak Roy
Working with excel files
R also comes with different packages to support read, write and
manipulate excel files directly without converting them in other
formats.
Some of the common packages used today are
ØXLConnect - uses rjava: a low level R to java interface
ØOpen.xlsx - uses C++ dependencies instead of rjava(java)
ØGdata - with pearl dependencies
ØReadXL, XLSX, readr packages.
Let’s learn each of them in detail.
Rupak Roy
XLCONNECT
ØXLCONNECT: is a connector for R that provides comprehensive
functionality to read, write and format Excel data.
ØImport functions include:
loadWorkbook()
readWorkbook()
readWorkbookFromFile()
ØExport functions inlude:
createSheet()
writeWorkSheet()
saveWorkbook()
Rupak Roy
XLCONNECT:loadWorkbook()
 loadWorkbook(): Loads or create a Microsoft excel workbook in R
for further manipulation.
>loadWorkbook(filename, create = FALSE, password = NULL)
Where
filename = excel workbook to be loaded
create = Specifies if the file should be created if it does not already
exist (default is FALSE)
password = Password to use when opening password protected files.
The default NULL means no password is being used. This
argument is ignored when creating new files using create = TRUE.
XLCONNECT:loadWorkbook()
#install the XLConnect package
>install.packages(“XLConnect”, dependencies = TRUE)
#load the functions from XLConnect package.
>library(XLConnect)
#load the excel file
>xlsx_data<- loadWorkbook("sample.xlsx")
>class(xlsx_data)
To know more about the features of loadWorkbook() use
>?XLConnect::loadWorkbook
Rupak Roy
XLCONNECT:readWorksheet ()
 readWorksheet(): Reads data from worksheets of a loadWorkbook.
>worksheet1<-readWorksheet(object, sheet, startCol, endRow, Header = T,….)
Where
object = name of the workbook from loadWorkbook
sheet = sheet name of the workbook
startCol = The index of the first column to read from. Defaults to 0 meaning that
the start column is determined automatically..
endRow = The index of the last row to read from. Defaults to 0 meaning that the
end row is determined automatically.
startRow = The index of the first row to read from. Defaults to 0 meaning that
the start row is determined automatically.
endCol = The index of the last column to read from. Defaults to 0 meaning that
the end column is determined automatically..
XLCONNECT:readWorksheet()
#install the XLConnect package
>install.packages(“XLConnect”, dependencies = TRUE)
#load the functions from XLConnect package.
>library(XLConnect)
#Read the 1st excel sheet from xlsx_data R object i.e. sample.xlsx file.
>excel_data<- readWorksheet (xlsx_data, “store”, header = T)
>View(excel_data)
#Read the 2nd excel sheet from xlsx_data R object i.e. sample.xlsx file.
>excel_data2<- readWorksheet (xlsx_data,“bike_sharing_program”, endRow = 10,
startCol =3, header = T)
>View(excel_data2)
To know more about the features of loadWorkbook() use
>?XLConnect::readWorksheet
XLCONNECT:readWorksheetFromFile()
 readWorksheetFromFile(): Reads data from a worksheet directly from a
physical excel file.
>worksheet3<-readWorksheetFromFile(file, sheet, startCol, endRow, Header =
T ……. Same Arguments passed to readWorksheet)
Where
file = name of the excel file to be read
sheet = sheet name of workbook
startCol = The index of the first column to read from. Defaults to 0 meaning that
the start column is determined automatically..
endRow = The index of the last row to read from. Defaults to 0 meaning that the
end row is determined automatically.
startRow = The index of the first row to read from. Defaults to 0 meaning that
the start row is determined automatically.
endCol = The index of the last column to read from. Defaults to 0 meaning that
the end column is determined automatically..
XLCONNECT:readWorksheetFromFile()
#install the XLConnect package
>install.packages(“XLConnect”, dependencies = TRUE)
#load the functions from XLConnect package.
>library(XLConnect)
#Read the excel sheet directly from an excel file
>excel_data3<- readWorksheetFromFile (“sample.xlsx”, “store”, header = T)
>View(excel_data3)
XLConnect::readWorksheetFromFile - the only difference between
readWorksheet and readWorksheetFromFile is that in readWorksheet() the
excel file have to be first loaded in R directory using loadWorkbook() in order
to view the data but in readWorksheetFromFile() it reads the excel sheet
directly from a physical file.
To know more about the features of readWorksheetFromFile() use
>?XLConnect::readWorksheetFromFile
XLCONNECT:createSheet()
 createSheet(): Creates new worksheet in a workbook loaded via
loadWorkbook()
>createSheet (object, name)
Where
object = name of the workbook to use
name = name of the sheet to create
Rupak Roy
XLCONNECT:createSheet()
#install the XLConnect package
>install.packages(“XLConnect”, dependencies = TRUE)
#load the functions from XLConnect package.
>library(XLConnect)
#Create the a new empty excel sheet in the workbook
>createSheet(xlsx_data, “new_sheet”)
XLConnect::createSheet() - Creates a worksheet with the specified name if it
does not already exist. The naming of worksheets needs to be in line with
Excel's convention, otherwise an exception will be thrown. For example,
worksheet names cannot be longer than 31 characters.
To know more about the features of createSheet() use
>?XLConnect::createSheet
Rupak Roy
XLCONNECT:writeWorksheet()
 writeWorksheet(): Creates new worksheet in a workbook loaded via
loadWorkbook()
>writeWorksheet (object, data, sheet=“sheet_name”)
Where
object = name of the worksheet to read
data = data to be written
sheet = The name or index of the sheet to write to
startRow = Index of the first row to write to. The default is startRow = 1
startCol = Index of the first column to write to. The default is startCol = 1
header = Specifies if the column names should be written. Default (TRUE).
XLCONNECT:writeWorksheet()
#install the XLConnect package
>install.packages(“XLConnect”, dependencies = TRUE)
#load the functions from XLConnect package.
>library(XLConnect)
#Write/Copy a workbook sheet directly to a new workbook sheet
>writeWorkSheet(xlsx_data, bike_sharing_program, “new_sheet”)
XLConnect::writeWorksheet() - Writes data to the worksheet specified
by sheet. Data here is assumed to be a data.frame and is coerced to one if this
is not already the case. StartRow and startCol define the top left corner of the
data region to be written.
To know more about the features of writeWorksheet() use
>?XLConnect::writeWorksheet
Rupak Roy
XLCONNECT:saveWorkbook()
 saveWorkbook(): Saves a workbook to the corresponding Excel file. This
method actually writes the workbook object to disk.
>saveWorkbook (object,file)
Where
object = the workbook to save
file = The file to which it will save the workbook ("save as")
>saveWorkbook(xlsx, “document1.xlsx”);
To know more about the saveWorkbook function use
?XLConnect::saveWorkbook
Rupak Roy
Next:
Troubleshooting XLConnect
Import export Excel files
Rupak Roy

More Related Content

PPT
Modern Fibres
PDF
moisture control and breathable finish
PDF
Viscose rayon fiber production process
PDF
Automotive Acoustics
PPT
Mca ii os u-1 introduction to os
PPTX
Chap2 RE processes
PPTX
Application of computer in TE. BUBT
Modern Fibres
moisture control and breathable finish
Viscose rayon fiber production process
Automotive Acoustics
Mca ii os u-1 introduction to os
Chap2 RE processes
Application of computer in TE. BUBT

Similar to Import and Export Excel files using XLConnect in R Studio (20)

PDF
Import and Export Excel Data using openxlsx in R Studio
PDF
Python openpyxl
PPTX
OpenpyXL Topic Notes in python perform excel operations
PDF
In java , I want you to implement a Data Structure known as a Doubly.pdf
PDF
How to Read Excel Files in Java (1).pdf
DOCX
LEX & YACC
PDF
Maxbox starter19
PPT
Linker scripts
PPTX
ADVANCE ITT BY PRASAD
PDF
Introduction to Latex
DOCX
EPiServer report generation
PPTX
Advance Mobile Application Development class 03
PDF
Linked List Static and Dynamic Memory Allocation
PPTX
latex-workshop Dr: Mohamed A. Alrshah
DOCX
Yacc topic beyond syllabus
PPT
py lsfgsdfgdsfgdfsgdsfgdfsfdgfdlecasdf.ppt
PPTX
Exploiting JXL using Selenium
DOCX
Sql loader good example
PPT
ComandosDePython_ComponentesBasicosImpl.ppt
Import and Export Excel Data using openxlsx in R Studio
Python openpyxl
OpenpyXL Topic Notes in python perform excel operations
In java , I want you to implement a Data Structure known as a Doubly.pdf
How to Read Excel Files in Java (1).pdf
LEX & YACC
Maxbox starter19
Linker scripts
ADVANCE ITT BY PRASAD
Introduction to Latex
EPiServer report generation
Advance Mobile Application Development class 03
Linked List Static and Dynamic Memory Allocation
latex-workshop Dr: Mohamed A. Alrshah
Yacc topic beyond syllabus
py lsfgsdfgdsfgdfsgdsfgdfsfdgfdlecasdf.ppt
Exploiting JXL using Selenium
Sql loader good example
ComandosDePython_ComponentesBasicosImpl.ppt
Ad

More from Rupak Roy (20)

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

Recently uploaded (20)

PDF
Basic Mud Logging Guide for educational purpose
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
Insiders guide to clinical Medicine.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Classroom Observation Tools for Teachers
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Cell Types and Its function , kingdom of life
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
Basic Mud Logging Guide for educational purpose
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPH.pptx obstetrics and gynecology in nursing
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Insiders guide to clinical Medicine.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Microbial diseases, their pathogenesis and prophylaxis
Classroom Observation Tools for Teachers
human mycosis Human fungal infections are called human mycosis..pptx
Anesthesia in Laparoscopic Surgery in India
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Cell Types and Its function , kingdom of life
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
VCE English Exam - Section C Student Revision Booklet
O7-L3 Supply Chain Operations - ICLT Program
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
01-Introduction-to-Information-Management.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?

Import and Export Excel files using XLConnect in R Studio

  • 1. Import export Excel files Using XLConnect Rupak Roy
  • 2. Working with excel files R also comes with different packages to support read, write and manipulate excel files directly without converting them in other formats. Some of the common packages used today are ØXLConnect - uses rjava: a low level R to java interface ØOpen.xlsx - uses C++ dependencies instead of rjava(java) ØGdata - with pearl dependencies ØReadXL, XLSX, readr packages. Let’s learn each of them in detail. Rupak Roy
  • 3. XLCONNECT ØXLCONNECT: is a connector for R that provides comprehensive functionality to read, write and format Excel data. ØImport functions include: loadWorkbook() readWorkbook() readWorkbookFromFile() ØExport functions inlude: createSheet() writeWorkSheet() saveWorkbook() Rupak Roy
  • 4. XLCONNECT:loadWorkbook()  loadWorkbook(): Loads or create a Microsoft excel workbook in R for further manipulation. >loadWorkbook(filename, create = FALSE, password = NULL) Where filename = excel workbook to be loaded create = Specifies if the file should be created if it does not already exist (default is FALSE) password = Password to use when opening password protected files. The default NULL means no password is being used. This argument is ignored when creating new files using create = TRUE.
  • 5. XLCONNECT:loadWorkbook() #install the XLConnect package >install.packages(“XLConnect”, dependencies = TRUE) #load the functions from XLConnect package. >library(XLConnect) #load the excel file >xlsx_data<- loadWorkbook("sample.xlsx") >class(xlsx_data) To know more about the features of loadWorkbook() use >?XLConnect::loadWorkbook Rupak Roy
  • 6. XLCONNECT:readWorksheet ()  readWorksheet(): Reads data from worksheets of a loadWorkbook. >worksheet1<-readWorksheet(object, sheet, startCol, endRow, Header = T,….) Where object = name of the workbook from loadWorkbook sheet = sheet name of the workbook startCol = The index of the first column to read from. Defaults to 0 meaning that the start column is determined automatically.. endRow = The index of the last row to read from. Defaults to 0 meaning that the end row is determined automatically. startRow = The index of the first row to read from. Defaults to 0 meaning that the start row is determined automatically. endCol = The index of the last column to read from. Defaults to 0 meaning that the end column is determined automatically..
  • 7. XLCONNECT:readWorksheet() #install the XLConnect package >install.packages(“XLConnect”, dependencies = TRUE) #load the functions from XLConnect package. >library(XLConnect) #Read the 1st excel sheet from xlsx_data R object i.e. sample.xlsx file. >excel_data<- readWorksheet (xlsx_data, “store”, header = T) >View(excel_data) #Read the 2nd excel sheet from xlsx_data R object i.e. sample.xlsx file. >excel_data2<- readWorksheet (xlsx_data,“bike_sharing_program”, endRow = 10, startCol =3, header = T) >View(excel_data2) To know more about the features of loadWorkbook() use >?XLConnect::readWorksheet
  • 8. XLCONNECT:readWorksheetFromFile()  readWorksheetFromFile(): Reads data from a worksheet directly from a physical excel file. >worksheet3<-readWorksheetFromFile(file, sheet, startCol, endRow, Header = T ……. Same Arguments passed to readWorksheet) Where file = name of the excel file to be read sheet = sheet name of workbook startCol = The index of the first column to read from. Defaults to 0 meaning that the start column is determined automatically.. endRow = The index of the last row to read from. Defaults to 0 meaning that the end row is determined automatically. startRow = The index of the first row to read from. Defaults to 0 meaning that the start row is determined automatically. endCol = The index of the last column to read from. Defaults to 0 meaning that the end column is determined automatically..
  • 9. XLCONNECT:readWorksheetFromFile() #install the XLConnect package >install.packages(“XLConnect”, dependencies = TRUE) #load the functions from XLConnect package. >library(XLConnect) #Read the excel sheet directly from an excel file >excel_data3<- readWorksheetFromFile (“sample.xlsx”, “store”, header = T) >View(excel_data3) XLConnect::readWorksheetFromFile - the only difference between readWorksheet and readWorksheetFromFile is that in readWorksheet() the excel file have to be first loaded in R directory using loadWorkbook() in order to view the data but in readWorksheetFromFile() it reads the excel sheet directly from a physical file. To know more about the features of readWorksheetFromFile() use >?XLConnect::readWorksheetFromFile
  • 10. XLCONNECT:createSheet()  createSheet(): Creates new worksheet in a workbook loaded via loadWorkbook() >createSheet (object, name) Where object = name of the workbook to use name = name of the sheet to create Rupak Roy
  • 11. XLCONNECT:createSheet() #install the XLConnect package >install.packages(“XLConnect”, dependencies = TRUE) #load the functions from XLConnect package. >library(XLConnect) #Create the a new empty excel sheet in the workbook >createSheet(xlsx_data, “new_sheet”) XLConnect::createSheet() - Creates a worksheet with the specified name if it does not already exist. The naming of worksheets needs to be in line with Excel's convention, otherwise an exception will be thrown. For example, worksheet names cannot be longer than 31 characters. To know more about the features of createSheet() use >?XLConnect::createSheet Rupak Roy
  • 12. XLCONNECT:writeWorksheet()  writeWorksheet(): Creates new worksheet in a workbook loaded via loadWorkbook() >writeWorksheet (object, data, sheet=“sheet_name”) Where object = name of the worksheet to read data = data to be written sheet = The name or index of the sheet to write to startRow = Index of the first row to write to. The default is startRow = 1 startCol = Index of the first column to write to. The default is startCol = 1 header = Specifies if the column names should be written. Default (TRUE).
  • 13. XLCONNECT:writeWorksheet() #install the XLConnect package >install.packages(“XLConnect”, dependencies = TRUE) #load the functions from XLConnect package. >library(XLConnect) #Write/Copy a workbook sheet directly to a new workbook sheet >writeWorkSheet(xlsx_data, bike_sharing_program, “new_sheet”) XLConnect::writeWorksheet() - Writes data to the worksheet specified by sheet. Data here is assumed to be a data.frame and is coerced to one if this is not already the case. StartRow and startCol define the top left corner of the data region to be written. To know more about the features of writeWorksheet() use >?XLConnect::writeWorksheet Rupak Roy
  • 14. XLCONNECT:saveWorkbook()  saveWorkbook(): Saves a workbook to the corresponding Excel file. This method actually writes the workbook object to disk. >saveWorkbook (object,file) Where object = the workbook to save file = The file to which it will save the workbook ("save as") >saveWorkbook(xlsx, “document1.xlsx”); To know more about the saveWorkbook function use ?XLConnect::saveWorkbook Rupak Roy