Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

openxlsx Error: length of rows and cols must be equal

Tags:

r

excel

openxlsx

I just ran into the same problem that this Nabble user ran into when trying to apply a style to an Excel workbook using a non-rectangular set of rows and columns.

wb <- createWorkbook()
addWorksheet(wb, "Iris")
writeData(wb, sheet = 1, x = iris)
boldStyle <- createStyle(textDecoration=c("bold"))
addStyle(wb, 1, style = boldStyle, cols=4:5, rows = 1:150)

Error in addStyle(wb, 1, style = boldStyle, cols = 4:5, rows = 1:150, : Length of rows and cols must be equal.

How can I fix this code so that I don't get an error and so that I can apply the style to a non-rectangular set of cells?

like image 888
D. Woods Avatar asked Aug 01 '17 03:08

D. Woods


1 Answers

One of the arguments for addStyle, gridExpand, is set to FALSE by default. The trick to get this do what you're attempting is to set this argument to TRUE. This allows the function to apply the style to the combination of rows and columns that were supplied.

Replace the last line with this one and it should work fine:

addStyle(wb, 1, style = boldStyle, cols=4:5, rows = 1:150, gridExpand = T)
like image 117
D. Woods Avatar answered Nov 01 '22 02:11

D. Woods