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:

After the installation the package is to be loaded:

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

Command 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

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

The 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

Write inside an Excel sheet with R using XLConnect

Add other sheets to the workbook

At this point a second sheet could be created

Command 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’.

excel-loadAndNewVar

Add another variable to an Excel sheet with R using XLConnect

Add an Excel formula to an xlsx sheet trough R

The empty column isCurrent could be populated with a formula that links the Input sheet with the Airquality sheet.

The function idx2col returns the correspondig excel letter for the index column. With the syntax

the variable letterDay contains the excel letter for the column ‘Day’

 

Run an Excel formula with R trough XLConnect

Run an Excel formula with R trough XLConnect

Read and modify with R an existing xlsx file

Suppose that we have to modify the xlsx file just created.

In this case

  • exc2 is a new XLC object created from the existing excel file (the old file Xls)
  • the new sheet OzonePlot is added to exc2 object
  • the new named region OzonePlot is creating starting from OzonePlot!$A$1 cell

Adding an R plot (as image) to Excel

After creating a new sheet it is possible to put in this sheet a picture of a graph created in R

The command addImage insert the png image of the graph just saved in the named region OzonePlot.

The result is represented in next figure:

Add an R plot to Excel using XLConnect

Add an R plot to Excel using XLConnect

 

Extra functions of XLConnect

In this brief article we don’t focus the attention on the excel layout but on the sheets content. For example we can automatically create an excel calculator with an input sheet and an output sheet managing all information in R.

XLConnect has also some features as defining cellstyles (data formats, borders, back fill colors..) , hide/unhide sheets, specyfing column width and row height, merged cells, auto-filters, style actions (using templates) and behavior with error cells.

Stay tuned to know the rest!

Related Post

Share: