The usual route for importing data from spreadsheet applications like Excel or OpenOffice into R involves first exporting the data in CSV format. A newer (c. 2011) and more efficient CRAN package, called XLConnect, facilitates reading an entire Excel workbook and manipulating worksheets and cells programmatically from within R.
XLConnect doesn’t require a running installation of Microsoft Excel or any other special drivers to be able to read and write Excel files. The only requirement is a recent version of a Java Runtime Environment (JRE). Moreover, XLConnect can handle older .xls (BIFF) as well as the newer .xlsx (Office Open XML) file formats. Internally, XLConnect uses Apache POI (Poor Obfuscation Implementation) to manipulate Microsoft Office documents.
As a simple demonstration, the following worksheet, from a Guerrilla Capacity Planning workbook, will be displayed in R.
First, the Excel workbook is loaded as an R object:
<br />require(XLConnect)<br />wb <- loadWorkbook("~/.../XLConnect/82scaling.xlsx")<br />
The structure of the workbook object is:
<br />> str(wb)<br />Formal class 'workbook' [package "XLConnect"] with 2 slots<br /> [email protected] filename: chr ".../XLConnect/82scaling.xlsx"<br /> [email protected] jobj :Formal class 'jobjRef' [package "rJava"] with 2 slots<br /> .. .. [email protected] jobj :<externalptr> <br /> .. .. [email protected] jclass: chr "com/miraisolutions/xlconnect/integration/r/RWorkbookWrapper"<br />
Next, the workbook object is converted to a data frame:
<br />df <- readWorksheet(wb, <br /> sheet = "SGI-NUMA", <br /> startCol = which(LETTERS=="A"), <br /> startRow = 3, <br /> endCol = which(LETTERS=="P")<br /> endRow = 15, <br />)<br />
which can be compared with the original worksheet (above):
<br /> Measured KRays.Sec RelCap Efficiency Inverse Fit Transform Trendline Parameters<br />1 CPU (p) X(p) C=X(p)/X(1) C/p p/C p-1 (p/C)-1 Quadratic Coefficients<br />2 1 20 1.00 1.00 1.00 0 0.00 a 5.0000E-06<br />3 4 78 3.90 0.98 1.03 3 0.03 b 0.0500<br />4 8 130 6.50 0.81 1.23 7 0.23 c 0.0000<br /> ...<br />
How to apply other XLConnect functions is described in the associated vignette.