Lorenzo Rigamonti
Lorenzo Rigamonti

Reputation: 1775

Exporting data through RODBC in Excel

After performing calculations in R we wanto to export the results in an Excel file in order to make some formatting and to add graphs. I fould the following page

http://learnr.wordpress.com/2009/10/06/export-data-frames-to-multi-worksheet-excel-file/

which reports the following code

> library(RODBC) 

> save2excel <- function(x) sqlSave(xlsFile,
     x, tablename = x$Species[1], rownames = FALSE)    

> xlsFile <- odbcConnectExcel("RODBC.xls", readOnly = FALSE)
> l_ply(testlist, save2excel)
> odbcCloseAll() 

I have some difficulties in implementing it in fact it returns an error like:

Errore in sqlSave(xlsFile, x, tablename = NULL, rownames = FALSE) : 
  should be a data frame

Do you have any hints?

Upvotes: 0

Views: 2371

Answers (2)

Jordan
Jordan

Reputation: 614

I used the same (great) blog post and ran into both of the same errors (data frame and excel file corrupted). Sounds like you got the data.frame part figured out but are still getting the corrupt file error. I had to remove the function aspect of the bloggers post to get rid of that error.

newdat <- data.frame(rnorm(100,0,10))

xlsFile <- odbcConnectExcel("Test.xls", readOnly = FALSE)
sqlSave(xlsFile,newdat, append=FALSE)
odbcCloseAll()

This should create a new worksheet, called "newdat" in the file called "Test.xls". If that file already exists, it will be a new worksheet. Otherwise it will create the file and be the only worksheet.

Upvotes: 0

jackStinger
jackStinger

Reputation: 2055

Yes. It looks like x is a list/vector.You can only save data frames as excel sheets. If you provide more info on your data structure, we could help you.

A most general approach, if your data in the list/vector form, it can be a prob, but if it is a logical table( matrix/data table etc) you can do an as.data.frame(x) to make it a DF.

Upvotes: 1

Related Questions