简体   繁体   中英

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?

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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM