Quickly read Excel (xlsx) worksheets into R on any platform

[This article was first published on Houses of Stones » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I wrote a couple days about about importing Excel files into R. There are lots of ways to do this, but all the ways that use only R have drawbacks (as I outlined in my last post), and all the other ways require installation of programs other than R. I’m not opposed to using programs other than R – it’s easy enough to weave, for example, Python and R code into each other. But I’d become curious about the possibility of solving this problem without the need for added programs, so I did some more searching. Turns out you can import an .xlsx document into pretty much anything that can parse XML, because that’s all an .xlsx document is.

The following gist uses R’s XML package, as well as a little help from the `plyr` package. It will import all sheets from an .xlsx document into a list of data frames and doesn’t require any additional installations of converters, and as far as I can tell, it should work on any platform. They key is to rename the document to have a .zip extension. Then you can unzip the document, pull out the XML files, and match them up to recreate a view similar to what you would see in Excel. The issue of how to deal with XML files in R is an issue that comes up regularly on Stack Overflow, so I thought this gist could also be a nice little example of how to handle that sort of problem (long story short: convert the XML to a list and manipulate the list – it’s generally much easier than trying to manipulate the XML directly).

EDIT 1: You may notice that this converts all times and date-times into dates. That’s because the particular data I was working with didn’t have times or date-times, and because I’m still trying to figure out exactly how Excel handles date/time origins. I’ll edit the function when I find out more.

EDIT 2: Ok, times and date-times are now appropriate, but only if you’re opening an xlsx document that was created on Windows. The Mac version of Excel apparently uses a different origin for dates, which is stupid. I’m trying to figure out how to tell from the XML which platform an xlsx document was created on, but so far I haven’t had any luck.

EDIT 3: I’ve now updated the function. Interestingly, it seems Microsoft might have standardized its handling of dates across operating systems. I created an .xlsx file on my Mac and only the Windows origin (“1899-12-30″) resulted in the correct dates. I’ve left a note in the code to indicate the origin that is supposed to be the right origin for Mac Excel (“1904-01-01″). I’ve also included some options in the function to select only specific sheets from a workbook, and to indicate whether the function should expect the sheets to have headers.

# The MIT License (MIT)
#
# Copyright (c) 2012 Schaun Jacob Wheeler
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
library(XML)
library(plyr)
library(pbapply)
xlsxToR <- function(file, keep_sheets = NULL, header = FALSE) {
temp_dir <- file.path(tempdir(), "xlsxToRtemp")
suppressWarnings(dir.create(temp_dir))
file.copy(file, temp_dir)
new_file <- list.files(temp_dir, full.name = TRUE, pattern = basename(file))
unzip(new_file, exdir = temp_dir)
# Get OS
# These lines are included because R documentation states that Excel handles
# date origins differently on Mac than on Windows. However, manual inspection
# of Excel files created on Windows and Mac indicated that in fact the origin
# is handled the same across both platforms. I've kept the original code here
# commented out in case it can be of use in the future.
# mac <- xmlToList(xmlParse(list.files(
# paste0(temp_dir, "/docProps"), full.name = TRUE, pattern = "app.xml")))
# mac <- grepl("Macintosh", mac$Application)
# if(mac) {
# os_origin <- "1899-12-30" # documentation says should be "1904-01-01"
# } else {
# os_origin <- "1899-12-30"
# }
# Get names of sheets
sheet_names <- xmlToList(xmlParse(list.files(
paste0(temp_dir, "/xl"), full.name = TRUE, pattern = "workbook.xml")))
sheet_names <- rbind.fill(lapply(sheet_names$sheets, function(x) {
as.data.frame(as.list(x), stringsAsFactors = FALSE)
}))
rownames(sheet_names) <- NULL
sheet_names <- as.data.frame(sheet_names,stringsAsFactors = FALSE)
sheet_names$id <- gsub("\\D", "", sheet_names$id)
# Get column classes
styles <- xmlParse(list.files(
paste0(temp_dir, "/xl"), full.name = TRUE, pattern = "styles.xml"))
styles <- xpathApply(styles, "//x:xf[@applyNumberFormat and @numFmtId]",
namespaces = "x", xmlAttrs)
styles <- lapply(styles, function(x) {
x[grepl("applyNumberFormat|numFmtId", names(x))]})
styles <- do.call("rbind", (lapply(styles,
function(x) as.data.frame(as.list(x[c("applyNumberFormat", "numFmtId")]),
stringsAsFactors = FALSE))))
if(!is.null(keep_sheets)) {
sheet_names <- sheet_names[sheet_names$name %in% keep_sheets,]
}
worksheet_paths <- list.files(
paste0(temp_dir, "/xl/worksheets"),
full.name = TRUE,
pattern = paste0(
"sheet(",
paste(sheet_names$id, collapse = "|"),
")\\.xml$"))
worksheets <- lapply(worksheet_paths, function(x) xmlRoot(xmlParse(x))[["sheetData"]])
worksheets <- pblapply(seq_along(worksheets), function(i) {
x <- xpathApply(worksheets[[i]], "//x:c", namespaces = "x", function(node) {
c("v" = xmlValue(node[["v"]]), xmlAttrs(node))
})
if(length(x) > 0) {
x_rows <- unlist(lapply(seq_along(x), function(i) rep(i, length(x[[i]]))))
x <- unlist(x)
x <- reshape(
data.frame(
"row" = x_rows,
"ind" = names(x),
"value" = x,
stringsAsFactors = FALSE),
idvar = "row", timevar = "ind", direction = "wide")
x$sheet <- sheet_names[sheet_names$id == i, "name"]
colnames(x) <- gsub("^value\\.", "", colnames(x))
}
x
})
worksheets <- do.call("rbind.fill",
worksheets[sapply(worksheets, class) == "data.frame"])
entries <- xmlParse(list.files(paste0(temp_dir, "/xl"), full.name = TRUE,
pattern = "sharedStrings.xml$"))
entries <- xpathSApply(entries, "//x:si", namespaces = "x", xmlValue)
names(entries) <- seq_along(entries) - 1
entries_match <- entries[
match(worksheets$v[worksheets$t == "s" & !is.na(worksheets$t)],
names(entries))]
worksheets$v[worksheets$t == "s" & !is.na(worksheets$t)] <- entries_match
worksheets$cols <- match(gsub("\\d", "", worksheets$r), LETTERS)
worksheets$rows <- as.numeric(gsub("\\D", "", worksheets$r))
if(!any(grepl("^s$", colnames(worksheets)))) {
worksheets$s <- NA
}
workbook <- lapply(unique(worksheets$sheet), function(x) {
y <- worksheets[worksheets$sheet == x,]
y_style <- as.data.frame(tapply(y$s, list(y$rows, y$cols), identity),
stringsAsFactors = FALSE)
y <- as.data.frame(tapply(y$v, list(y$rows, y$cols), identity),
stringsAsFactors = FALSE)
if(header) {
colnames(y) <- y[1,]
y <- y[-1,]
y_style <- y_style[-1,]
}
y_style <- sapply(y_style, function(x) {
out <- names(which.max(table(x)))
out[is.null(out)] <- NA
out
})
if(length(styles) > 0) {
y_style <- styles$numFmtId[match(y_style, styles$applyNumberFormat)]
}
y_style[y_style %in% 14:17] <- "date"
y_style[y_style %in% c(18:21, 45:47)] <- "time"
y_style[y_style %in% 22] <- "datetime"
y_style[is.na(y_style) & !sapply(y, function(x)any(grepl("\\D", x)))] <- "numeric"
y_style[is.na(y_style)] <- "character"
y_style[!(y_style %in% c("date", "time", "datetime", "numeric"))] <- "character"
y[] <- lapply(seq_along(y), function(i) {
switch(y_style[i],
character = y[,i],
numeric = as.numeric(y[,i]),
date = as.Date(as.numeric(y[,i]), origin = os_origin),
time = strftime(as.POSIXct(as.numeric(y[,i]), origin = os_origin), format = "%H:%M:%S"),
datetime = as.POSIXct(as.numeric(y[,i]), origin = os_origin))
})
y
})
if(length(workbook) == 1) {
workbook <- workbook[[1]]
} else {
names(workbook) <- sheet_names$name
}
workbook
}
view raw xlsxToR.r hosted with ❤ by GitHub

To leave a comment for the author, please follow the link and comment on their blog: Houses of Stones » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)