1

I have a data frame and I am using the below code

wb <- createWorkbook(type = "xlsx")
sheet1 <- createSheet(wb, sheetName = "sheet1")
addDataFrame(df, sheet1, col.names = TRUE, row.names = FALSE, startRow = 1)
saveWorkbook(wb, file = "filename.xlsx")

I have already set the directory through R studio so need not to mention it explicitly. I have gone through the documentation of write.xlsx which give an error commonly found on the web. The issue is when I download and pen the workbook I see the data like this

Column1 Column2 Column3 Column4 Column5 Column6
c(value1,value2,……) c(value1,value2,……) c(value1,value2,……) c(value1,value2,……) c(value1,value2,……) c(value1,value2,……)

I only find two rows, first row gives the column names and second row has all values of respective column in vector format (i.e. enclosed in c()). I do not want to row.names = TRUE as it would create an extra column with the row numbers.

same problem happens with write.xlsx2 function Any help would be much appreciated.

ARIMITRA MAITI
  • 303
  • 3
  • 4
  • 14
  • You could improve the question. Please hover over the R tag - it asks for a minimal reproducible example. [Here's a guide](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#answer-5963610). I suggest editing your posting accordingly. A good one usually provides minimal input data, the desired output data, code tries incl required packages - all copy-paste-run'able in a new/clean R session. *Why?* It makes it easier for all to follow and participate without guesswork. For example, what does `df` look like - nested lists? – lukeA Jan 05 '17 at 12:13
  • `c(value1,value2,……)` often indicates that you got lists in your data frame columns as in e.g. `df <- data.frame(x=I(list(letters[1:2], LETTERS[1:2])), y=1:2)` – lukeA Jan 05 '17 at 12:14
  • @lukeA Thank you for your help. I am using dplyr to process the data. I didnt realize class(df) from a dplyr shows "grouped_df" "tbl_df" "tbl" "data.frame" When I use str(df) it did show data_frame properties in a list. However if I write data.frame(df) and then use it in saveWorkbook, it works fine as expected. – ARIMITRA MAITI Jan 05 '17 at 13:15

1 Answers1

2

I don't think the issue is during download. Rather it's during addDataFrame(). While adding your df, you will need to coerce your dataframe as a list. Example:

addDataFrame(list(df), sheet1, col.names = TRUE, row.names = FALSE, startRow = 1)
Digvijay Sawant
  • 1,049
  • 3
  • 16
  • 32