From: Don Armstrong Date: Mon, 31 Oct 2016 22:14:56 +0000 (-0500) Subject: add array to excel X-Git-Url: https://git.donarmstrong.com/?p=r%2Fcommon_r_code.git;a=commitdiff_plain;h=ef0c4b67e16a3c311fe6182a4d5bb2de4df3d636 add array to excel --- diff --git a/array_to_excel.R b/array_to_excel.R new file mode 100644 index 0000000..b818ca0 --- /dev/null +++ b/array_to_excel.R @@ -0,0 +1,35 @@ +### this requires txt2xls, which you can find here: +### http://git.donarmstrong.com/bin.git/b/txt2xls + +mkdtemp <- function() { + max.tries <- 100 + while (max.tries > 0) { + tmpnam <- tempfile("dir") + if (dir.create(tmpnam)) { + return(tmpnam) + } + max.tries <- max.tries - 1 + } + return(NA) +} + +array_to_excel <- function(data,workbook) { + if (is.data.frame(data) || is.data.frame(data)) { + temp <- data + data <- list() + data[[workbook]] <- + temp + } + temp.dir <- mkdtemp() + worksheets <- NULL + for (worksheet in names(data)) { + write.table(data[[file]], + file=file.path(temp.dir,worksheet)) + worksheets <- c(worksheets, + file.path(temp.dir,worksheet)) + } + system("txt2xls","--output",workbook,worksheets) + unlink(worksheets) + unlink(temp.dir) + return(workbook) +}