These are two related follow-up questions to Write from R into template in excel while preserving formatting that I am writing here in a new thread, as I cannot comment the answers in the other one.
Actually Joris Meys answer/code (of the 27th June) does not work for me and I tried this already several times.
So again: the aim is to write with R an excel file while using the formatting in a template-file or just preserving it. Two different tools were mentioned in the answer: XLConnect and xlsx.
Joris Meys answer is based on XLConnect. But using the exact same code my result is that the cells that are filled with new content become white and without any preserved formatting. All the other cells (where no new content is inserted) indeed take over the previous style.
I'm using the newest RGui (32-bit) V. 2.15.1 in WinXP and the newest versions of XLConnect and xlsx (and I also just checked that all the other packages in R are up to date).
Ok a second question related to that: The thread mentions xlsx as an alternative. This information seems to me erroneous, too. I checked the website and the developer seems to be working on a solution on that. Currently I only see the possibility in xlsx to write the formatting manually in code, for example like this:
cs2 <- CellStyle(wb) + Font(wb, name="Courier New", isBold=TRUE) + # add a Font Borders(col="blue", position=c("TOP", "BOTTOM"), pen="BORDER_THICK") + # add borders Alignment(h="ALIGN_RIGHT")
(Source: http://code.google.com/p/rexcel/wiki/LowLevelAPI)
It would be really cool if someone could give a way of writing excel-files while preserving formats with R.
Edit: This is the code (only difference "Sheet1" instead of "aSheet" but I also tried other variants.
require(XLConnect)
wb <- loadWorkbook("test.xlsx", create=TRUE)
setStyleAction(wb,XLC$"STYLE_ACTION.NONE")
Data <- data.frame(
a = 1:10,
b = letters[1:10]
)
writeWorksheet(wb,Data,"Sheet1",startRow=1,startCol=1,header=TRUE)
saveWorkbook(wb)