I have come across the following problem. A lot of data is written to an Excel file. Within the written excel table, I want to set the cell color to a non-predefined value (which is a function of the number written to the cell). E.g.: The higher the number in a given cell, the greener the cell.
I know that solutions exist for the package xlsx
(See HERE and HERE). But I already use XLConnect
throughout the entire project and do not want to convert all of the code that I have so far.
Currently, I use the following code to set the cell color:
# create the excel workbook
wb <- loadWorkbook("FILENAME.xls", create=TRUE)`
# Create a CellStyle with yellow solid foreground
CellColor <- createCellStyle(wb)
setFillPattern(CellColor, fill = XLC$"FILL.SOLID_FOREGROUND")
setFillForegroundColor(CellColor, color = XLC$"COLOR.YELLOW")
# apply the CellStyle to a given cell, here: (10,10)
setCellStyle(wb, sheet=SHEETNAME, row=10, col=10, cellstyle=CellColor)
# save the workbook
saveWorkbook(wb)
Obviously, the problematic part is
color = XLC$"COLOR.YELLOW"
because it does not let me set the rgb code of the color I like. Attempts like
color = rgb(0.2,0.4,0.8)
fail.
The XLConnect documentation on page 91 does only tell that
The color is normally specified via a corresponding color constant from the XLC object.
There is no explanation on how to use RGB code.