shasj
shasj

Reputation: 85

Function in R saving in *.xls

I created a program that breaks a database in several other banks. But in this program I save this several banks in txt so now I'd like save in xls, but I don't know how.

I tried for (nm in Nms) write.table(Res[[nm]], paste(nm, 'xls', sep='.'), sep="\t",dec=",",col.names=TRUE, row.names=FALSE, quote=TRUE, na="NA")

but it did not work

decup <- function(dados,var){

require(gdata)

dados <- read.xls("dados.xls")

attach(dados)

Res = split(dados, var)

for (nm in Nms) write.table(Res[[nm]], file=paste(nm, 'txt', sep='.'))

for (nm in Nms) zip(paste(nm,'zip',sep='.'),paste(nm,'xls',sep='.'), zip = Sys.getenv("R_ZIPCMD", "zip"))

}

Upvotes: 1

Views: 391

Answers (1)

agstudy
agstudy

Reputation: 121568

You can use library XLConnect to read/write .xlsx files and writeWorksheet let's you save the your data.frames:

library(XLConnect)
## open workbook or create it if doesn't exist
wb <- loadWorkbook("writeWorksheet.xlsx", create = TRUE)
## for each data.frame create a sheet with its data
lapply(seq_along(Res), function(x)
   createSheet(wb, name = paste0("sheet",x))
   writeWorksheet(wb, Res[[x]], sheet = paste0("sheet",x), startRow = 4, startCol = 2)
 }

# Save workbook (this actually writes the file to disk)
saveWorkbook(wb)

EDIT to save a data.frame by workbook you do this :

lapply(seq_along(Res), function(x){
  wb <- loadWorkbook(paste0("database",x), create = TRUE)
  createSheet(wb, name ='sheet1' )
  writeWorksheet(wb, Res[[x]], sheet = 'sheet1', startRow = 4, startCol = 2)
  saveWorkbook(wb)
})

Upvotes: 1

Related Questions