By MilanoR
Excel is the basic tool for any type of analysis.
Often using R we have the need to communicate with Excel, for example
- read an excel file with multiple sheets
- write an excel file with multiple sheets
- read/write formulas
- export the results of an analysis in an excel report
XLConnect permits to create a formatted spreadsheet usable as a dynamic report of the R analysis.
The package installation is very easy:
install.packages("XLConnect")
After the installation the package is to be loaded:
require("XLConnect")
XLConnect offers many features to realize a good connection between R and Excel.
An Excel data grid becomes an R data.frame!
An Excel named region is reported in R and used to place R objects in the right position!
Reading and writing named ranges enables to process complex inputs and outputs in an efficient way.
We can introduce XLConnect by few sequential examples.
Create a new empty xlsx file trough R
We want to create a new empty .xlsx file with one empty sheet named ‘Input'; the syntax is
fileXlsCommand loadWorkbook creates an R workbook object.
Command saveWorkbook save the R object in a xlsx file.
The result is represented in next figure:
Create an Excel empty sheet trough R using XLConnect
Populate an empty xlsx sheet with R
We want to add something to the empty sheet input
inputThe input data.frame, with 2 rows and 2 column, is created and the command writeWorksheet writes the content of this data.frame in the input sheet starting from the cell (1,2).

Write inside an Excel sheet with R using XLConnect
Add other sheets to the workbook
At this point a second sheet could be created
require(reshape) createSheet(exc,'Airquality') airquality$isCurrentCommand createName create a named region ‘Airquality' starting from the cell $A$1 of sheet Airquality. Command writeNamedRegion writes airquality data.frame with headers in the named region ‘Airquality'.
<div …read more
Source:: r-bloggers.com