From this post I got a script that exports the list as separate sheets in an Excel file (code as shown below). Now I would like to wrap it in a convenient function to reproduce this behavior by specifying the name of the input list and the name of the output file.
Sample data:
var1 <- list('2003' = 1:3, '2004' = c(4:3), '2005' = c(6,4,1), '2006' = 1:4 )
Current script:
require("XLConnect")
wb <- loadWorkbook("var1.xlsx", create = TRUE)
createSheet(wb, names(var1))
writeWorksheet(wb, var1, names(var1),header=FALSE)
saveWorkbook(wb)
Disclaimer: While I am blushing to ask such a simple question, I am sure that many other SO visitors will be happy to find this information here: 7)
EDIT :: ready-to-use function:
save.excel <-function(.list, default = 'var1', path = ''){
require("XLConnect")
.name <- as.list(match.call())[2]
if(is.language(.name[[1]])) wb_name <- paste0(paste0(path, default, collapse = '/'), '.xlsx')
if(is.symbol(.name[[1]])) wb_name <- paste0(paste0(path, as.character(.name), collapse = '/'), '.xlsx')
wb <- loadWorkbook(wb_name, create = TRUE)
createSheet(wb, names(.list))
writeWorksheet(wb,.list, names(.list),header=FALSE)
saveWorkbook(wb)
}
The only difference from the solution below is that I added XLConnect as the library requested inside the function, in case you didn't do it manually before; 7)