简体   繁体   中英

How to save an R data frame to Excel file with certain cells in bold?

I often write papers with correlation matrices. I would like to be able to export the correlation matrix to Excel in xls or xlsx format. I would also like to format in bold correlations that meet a threshold (eg, > .2). I was thinking maybe XLConnect might provide the functionality.

In order to make the example simple, assume the data frame is as follows, and assume I want to bold all cells greater than 5.

x <- data.frame(matrix(1:9, nrow = 3))
# > x
#   X1 X2 X3
# 1  1  4  7
# 2  2  5  8
# 3  3  6  9

As an aside, I note that solutions have been proposed for cellbolding for markdown:

I've also found this answer, but it is not a very general solution in that it takes quite a bit to adapt it to the general task of taking a data frame and a formatting rule:

export data frames to Excel via xlsx with conditional formatting

I created the following function that was adapted from @jota's answer here

xlsx_boldcells <- function(x, matches, file = "test.xlsx", sheetname = "sheet1") {
    # x data.frame or matrix
    # matches: logical data.frame or matrix of the same size indicating which cells to bold
    # copy data frame to work book and load workbook
    require(xlsx)
    write.xlsx(x, file, sheetName=sheetname)
    wb <- loadWorkbook(file)              

    # specify conditional formatting
    # Note: this could be modified to apply different formatting
    # see ?CellStyle
    fo <- Font(wb, isBold = TRUE)  
    cs <- CellStyle(wb, font=fo)  

    # Get cell references
    sheets <- getSheets(wb)               # get all sheets
    sheet <- sheets[[sheetname]]          # get specific sheet
    rows <- getRows(sheet, rowIndex=2:(nrow(x)+1))  # get rows
    cells <- getCells(rows, colIndex = 2:(ncol(x)+1))  

    # Matches to indexes
    indm <- data.frame(which(matches, arr.ind = TRUE, useNames = FALSE)) 
    names(indm) <- c("row", "col")
    # +1 required because row and column names occupy first rows and columns
    indm$index <- paste(indm$row + 1, indm$col + 1, sep = ".")

    # apply cell style
    lapply(indm$index, function(ii) setCellStyle(cells[[ii]],cs))

    # save workbook
    saveWorkbook(wb, file)
}

Thus, it can be applied to proposed problem:

 xlsx_boldcells(x, x > 5)

yielding:

excel表格大胆

Or it could be applied to the common correlation problem (ie, bolding large correlations, eg, greater than .6) as follows:

data(mtcars)
cors <- round(cor(mtcars), 2)
xlsx_boldcells(cors, abs(cors) > .6 & cors!=1, file = "cormatrix.xlsx")

使用xlsx的粗体单元格格式化相关性

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