What about using openxlsx?
library(openxlsx)
wb <- createWorkbook() # Save the workbook
addWorksheet(wb, 1)
main_headers <- c("", "", rep("value", 10), rep("share", 10)) # Top row
sub_headers <- c("year", "type", rep(LETTERS[1:10], 2)) # Bottom row
# Write headers
writeData(wb, 1, matrix(main_headers,ncol=length(main_headers)),
startCol = 1,
startRow = 1,
colNames = FALSE)
writeData(wb, 1,
matrix(sub_headers,ncol=length(sub_headers)),
startRow = 2,
startCol = 1,
colNames = FALSE)
mergeCells(wb, 1, cols = 3:12, rows = 1) # Merge "value" columns
mergeCells(wb, 1, cols = 13:22, rows = 1) # Merge "share" columns
writeData(wb, 1, my_table, startRow = 3, colNames = FALSE) # Define headers
saveWorkbook(wb, "Multiheaded_Table.xlsx", overwrite = TRUE) # Save the workbook