XLConnect
XLConnect
XLConnect
Contents
1 Introduction 1
1.1 Scope and purpose of this document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Introduction to XLConnect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2 Installation 2
2.1 Software Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.2 Package Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
i
5 What’s new in XLConnect version 1.0.5 13
ii
1 Introduction
1.1 Scope and purpose of this document
This document is a user manual for the XLConnect R package. It is meant to be a top-level introduction
and some of the more advanced features of XLConnect are not presented here. For such details, please refer
to the Reference Manual.
Examples
Any examples presented in this vignette can be found in the XLConnect.R script file, which you can find
in the top level library directory. You can also open the document by using the following command:
edit(file = system.file("XLConnect.R", package = "XLConnect"))
1 For more information on the Apache POI API, see the https://poi.apache.org/ webpage.
1
2 Installation
2.1 Software Requirements
XLConnect is completely cross-platform and as such runs under Windows, Unix/Linux and Mac (32- and
64-bit). It does not require an installation of Microsoft Excel, or any special drivers.
All you need to use XLConnect are the following:
❼ R, version 2.10.0 or higher
❼ Java Runtime Environment (JRE), version 6 or higher
library ( XLConnect )
The Reference Manual, containing help pages for each function within the package, can be opened by using
the help() command.
help ( XLConnect )
2
Load an Excel workbook (create if not existing)
3
Write a named range into a workbook
4
Then, via createSheet(), a sheet named ”chickSheet” is created within the workbook. We then use
writeWorksheet() to write the ChickWeight data frame into chickSheet.
At the end, we use saveWorkbook() to save the XLConnectExample1.xlsx file.
> require(XLConnect)
> wb <- loadWorkbook("XLConnectExample1.xlsx", create = TRUE)
> createSheet(wb, name = "chickSheet")
> writeWorksheet(wb, ChickWeight, sheet = "chickSheet", startRow = 3, startCol = 4)
> saveWorkbook(wb)
Please note that only at the point when we call saveWorkbook(), the Excel file is written to disk. All the
previous operations are performed in-memory, which has great performance advantages.
5
> require(XLConnect)
> wb <- loadWorkbook("XLConnectExample3.xlsx", create = TRUE)
> createSheet(wb, name = "womenData")
> createName(wb, name = "womenName", formula = "womenData!$C$5", overwrite = TRUE)
> writeNamedRegion(wb, women, name = "womenName")
> saveWorkbook(wb)
Figure 2: women dataset written into womenName named region in the XLConnectExample3.xlsx file
6
3.7 Example: writing a named region with one call
The writeNamedRegionToFile() function can be used to produce the same result as in the previous example,
with only one function call:
> require(XLConnect)
> writeNamedRegionToFile("XLConnectExample4.xlsx", women, name = "womenName",
+ formula = "womenData!$C$5")
7
3.8.4 readNamedRegionFromFile() - reading named regions of an Excel file
readNamedRegionFromFile() is a convenient wrapper function which allows for reading named regions from
an Excel file with one call. The function subsequently calls loadWorkbook() and readNamedRegion().
The file argument specifies the path to the Excel file to be read and name - the named region to be read.
The header argument specifies whether the first row of data should be interpreted as column names.
8
2 59 117
3 60 120
4 61 123
5 62 126
6 63 129
7 64 132
8 65 135
9 66 139
10 67 142
11 68 146
12 69 150
13 70 154
14 71 159
15 72 164
3.12 Example: reading from an Excel named region with one call
The result obtained in the previous example can be reproduced by calling the wrapper function:
readNamedRegionFromFile(), witout the necessity of loading the workbook first:
> require(XLConnect)
> data <- readNamedRegionFromFile("XLConnectExample3.xlsx", "womenName")
install . packages ( c (" XLConnect " , " zoo " , " ggplot2 "))
9
> wb <- loadWorkbook(wbFilename, create = TRUE)
> # Create a new sheet named ✬Swiss_Franc✬
> sheet <- "Swiss_Franc"
> createSheet(wb, name = sheet)
> # Create a new Excel name referring to the top left corner
> # of the sheet ✬Swiss_Franc✬ - this name is going to hold
> # our currency data
> dataName <- "currency"
> nameLocation <- paste(sheet, "$A$1", sep = "!")
> createName(wb, name = dataName, formula = nameLocation)
> # Instruct XLConnect to only apply a data format for a cell
> # but not to apply any other cell styling
> setStyleAction(wb, XLC$"STYLE_ACTION.DATA_FORMAT_ONLY")
> # Set the default format for numeric data to display
> # four digits after the decimal point
> setDataFormatForType(wb, type = XLC$"DATA_TYPE.NUMERIC", format = "0.0000")
> # Write the currency data to the named region created above
> # Note: the named region will be automatically redefined to encompass all
> # written data
> writeNamedRegion(wb, data = curr, name = dataName, header = TRUE)
> # Save the workbook (this actually writes the file to disk)
> saveWorkbook(wb)
Now, we are going to enhance the report further. Let’s say that for each currency we want to highlight the
points in time when there was a change of more than 2% compared to the previous day.
> # Load the workbook created above
> wb <- loadWorkbook(wbFilename)
> # Create a cell style for the header row
> csHeader <- createCellStyle(wb, name = "header")
> setFillPattern(csHeader, fill = XLC$FILL.SOLID_FOREGROUND)
> setFillForegroundColor(csHeader, color = XLC$COLOR.GREY_25_PERCENT)
> # Create a date cell style with a custom format for the Date column
> csDate <- createCellStyle(wb, name = "date")
> setDataFormat(csDate, format = "yyyy-mm-dd")
> # Create a highlighting cell style
> csHlight <- createCellStyle(wb, name = "highlight")
> setFillPattern(csHlight, fill = XLC$FILL.SOLID_FOREGROUND)
> setFillForegroundColor(csHlight, color = XLC$COLOR.CORNFLOWER_BLUE)
> # Apply header cell style to the header row
> setCellStyle(wb, sheet = sheet, row = 1,
+ col = seq(length.out = ncol(curr)),
+ cellstyle = csHeader)
> # Index for all rows except header row
> allRows <- seq(length = nrow(curr)) + 1
> # Apply date cell style to the Date column
> setCellStyle(wb, sheet = sheet, row = allRows, col = 1,
+ cellstyle = csDate)
> # Set column width such that the full date column is visible
> setColumnWidth(wb, sheet = sheet, column = 1, width = 2800)
> # Check if there was a change of more than 2% compared
> # to the previous day (per currency)
> idx <- rollapply(curr[, -1], width = 2,
+ FUN = function(x) abs(x[2] / x[1] - 1),
+ by.column = TRUE) > 0.02
10
> idx <- rbind(rep(FALSE, ncol(idx)), idx)
> widx <- lapply(as.data.frame(idx), which)
> # Apply highlighting cell style
> for (i in seq(along = widx)) {
+ if (length(widx[[i]]) > 0) {
+ setCellStyle(wb, sheet = sheet, row = widx[[i]] + 1, col = i + 1,
+ cellstyle = csHlight)
+ }
+
+ # Note:
+ # +1 for row since there is a header row
+ # +1 for column since the first column is the time column
+ }
> saveWorkbook(wb)
As a next step, let’s add a graph to our report.
> wb <- loadWorkbook(wbFilename)
> # Stack currencies into a currency variable (for use with ggplot2 below)
> currencies <- names(curr)[-1]
> gcurr <- reshape(curr, varying = currencies, direction = "long", v.names = "Value",
+ times = currencies, timevar = "Currency")
> # Create a png graph showing the currencies in the context
> # of the Swiss Franc
> png(filename = "swiss_franc.png", width = 800, height = 600)
> p <- ggplot(gcurr, aes(Date, Value, colour = Currency)) +
+ geom_line() + stat_smooth(method = "loess") +
+ scale_y_continuous("Exchange Rate CHF/CUR") +
+ labs(title = paste0("CHF vs ", paste(currencies, collapse = ", ")),
+ x = "") +
+ theme(axis.title.y = element_text(size = 10, angle = 90, vjust = 0.3))
> print(p)
> dev.off()
> # Define where the image should be placed via a named region;
> # let✬s put the image two columns left to the data starting
> # in the 5th row
> createName(wb, name = "graph",
+ formula <- paste(sheet, idx2cref(c(5, ncol(curr) + 2)), sep = "!"))
> # Note: idx2cref converts indices (row, col) to Excel cell references
>
> # Put the image created above at the corresponding location
> addImage(wb, filename = "swiss_franc.png", name = "graph",
+ originalSize = TRUE)
> saveWorkbook(wb)
As a result, we obtain the following graph:
11
CHF vs EUR, USD, GBP
1.1
Exchange Rate CHF/CUR
1.0
Currency
EUR
0.9
GBP
USD
0.8
0.7
Jan 2014 Apr 2014 Jul 2014 Oct 2014 Jan 2015
Check out the Excel file to see how the data and the graph have been exported!
12
5 What’s new in XLConnect version 1.0.5
We have introduced the following updates in version 1.0.5 of XLConnect:
❼ Upgraded XLConnectJars dependency to version 0.2-15
❼ Rely on Java version check from XLConnectJars which now supports Java 10
A summary of news in XLConnect 1.0.5 can be found in the NEWS file on CRAN.
writeNamedRegionToFile ( file = " huge . xls " , data = giant . data . frame ,
namedRegion = " LargeRegion " , formula = " LotsOfData ! A1 " )
Answer:
This is caused by the fact that XLConnect needs to copy your entire data object over to the JVM in order
to write it to a file and the JVM has to be initialized with a fixed upper limit on its memory size. To change
this amount, you can pass parameters to R’s JVM just like you can to a command line Java process via
rJava’s options support:
Note, however, that these parameters are evaluated exactly once per R session when the JVM is initialized
- this is usually once you load the first package that uses Java support, so you should do this as early as
possible.
13
Some general Java advice: The upper limit of the Xmx parameter is system dependent - most prominently,
32bit Windows will fail to work with anything much larger than 1500m, and it is usually a bad idea to set
Xmx larger than your physical memory size because garbage collection and virtual memory do not play well
together.
Answer:
XLConnect does not currently allow direct access to low-level formatting options. However, it is possible to
assign named cell styles to cells, so the preferred workflow would be to:
1. define some named cell styles in an Excel document (”Format” -> ”Styles” in pre-2007 Excel, ”Cell
styles” on the default pane of Excel 2007 and on)
2. save the document to a file
3. then load this template in XLConnect and use the setCellStyle method to assign the predefined styles:
library ( XLConnect )
w <- loadWorkbook ( " template . xls " )
df <- data . frame ( " foo " )
c <- getCellStyle (w , " FatFont " )
or
14
Error : NotImplementedException ( Java ): Error evaluating cell Sheet1 ! A1
Answer:
This type of error is triggered when XLConnect can not determine the value of a cell in the region you’re
trying to import. Usually, this happens because Apache POI does not support all possible Excel formu-
lae.
There is no direct solution for accessing the values of fields that Apache POI doesn’t know how to compute.
However, if you can live without the cell values and just want to ignore uncomputable cells, have a look at
the onErrorCell function to tell XLConnect that you want to ignore errors.
If you have Excel and are willing to invest some manual effort, you can manually create a static copy, that
can be imported as follows:
❼ Select the region containing your data
❼ Edit -> Copy
❼ Select an empty cell and Edit -> Paste Special
❼ In the Paste radio group, select Values
You should then be able to import the pasted region without problems.
Please log any bug reports, feature requests or technical inquiries at:
https://github.com/miraisolutions/xlconnect/issues
For any other feedback you may have, please contact us at:
[email protected]
15
5. XLConnect on METACRAN:
https://www.r-pkg.org/pkg/XLConnect
6. XLConnect on crantastic!:
https://crantastic.org/packages/XLConnect
7. XLConnect in the R Programming section on Wikibooks:
https://en.wikibooks.org/wiki/R Programming/Importing and exporting data
8. Quick Intro to XLConnect:
https://altons.github.io/rstats/2015/02/13/quick-intro-to-xlconnect/
9. ”How to Read Data from Excel into R”, article by Joris Meys and Andrie de Vries:
https://www.dummies.com/how-to/content/how-to-read-data-from-excel-into-r.html
10. R and Excel: Making Your Data Dumps Pretty with XLConnect:
https://www.youtube.com/watch?v=G3mOUbZr4HI
https://amunategui.github.io/excel-data-dumps/
16