Create bold column names (colnames) when writing to excel file in R

Viewed 1072

I'm trying to make my column name bold but I haven't found any answer. I created a sheet and added the columns names with colnames. And now I would like to make those new names bold, this is my code:

    file0<-read_excel("path", sheet=1)
tempermax <- read.csv("path")
tempmaxima <- rbind(file0,tempermax)
tempmaxima$Fecha <-as.Date(tempmaxima$Fecha, "%d/%m/%y") 
tempmaxima <- unique(tempmaxima)
colnames(tempmaxima)<- c("Fecha", "Temperatura_máxima_°_C")

write.xlsx(tempmaxima, file="path", 
           sheetName = 'Temperatura máxima', row.names = T, col.names = T)
1 Answers

One approach is to use a custom header style with the package openxlsx:

library(openxlsx)
workbook <- createWorkbook()
addWorksheet(workbook, "mtcars")
bold.style <- createStyle(textDecoration = "Bold")
writeData(workbook, "mtcars", mtcars, startRow = 1, startCol = 1,
          headerStyle = bold.style)
openXL(workbook)

enter image description here

See the formatting guide from the package for more info.

Related