Michael
Michael

Reputation: 575

Add a line of text to Excel sheet in openxlsx in R

Consider the following code:

install.packages("openxlsx") library(openxlsx) d <- data.frame(replicate(2,sample(0:1,10,rep=TRUE))) write.xlsx(d, "test.xlsx")

However, I want to add a line of text in the top of Excel sheet. I know I can use cat(paste0(), file = ) with write.table, but I am not sure with openxlsx.

Can anyone help me?

Upvotes: 3

Views: 6132

Answers (1)

Johnny
Johnny

Reputation: 771

You can write "text" to your workbook the same way you can write data to your workbook.

library(openxlsx)

d <- data.frame(replicate(2,sample(0:1,10,rep=TRUE)))

wb <- createWorkbook()
addWorksheet(wb, "Sheet1")

writeData(wb, "Sheet1", "This is an example", startCol = 1, startRow = 1)
writeData(wb, "Sheet1", d, startCol = 1, startRow = 3, rowNames = TRUE)

saveWorkbook(wb, "test.xlsx", overwrite = TRUE)

The code above produces the following Excel file:

enter image description here

Upvotes: 12

Related Questions