X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=array_to_excel.R;fp=array_to_excel.R;h=b818ca05c4b2f39e41acfc1265ab713cca6f9b4f;hb=ef0c4b67e16a3c311fe6182a4d5bb2de4df3d636;hp=0000000000000000000000000000000000000000;hpb=570e43f262a255d2da94a6f95decb10d550a170d;p=r%2Fcommon_r_code.git 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) +}