我有这样的数据
df<-structure(list(X = structure(3:1, .Label = c("CQLSKGQSYSVNVTFTSNIQSKSSKAVVHGILMGVP",
"KLALQLHPDRNPDDPQAQEKFQDLGAAYEVLSDSEKRKQYD", "MVEAIVEFDYQAQHDDELTISVGEIITNIRKEDGGWW"
), class = "factor")), class = "data.frame", row.names = c(NA,
-3L))
我试图突出显示每一行中的字母D,然后将其保存在xlsx中。我尝试使用此Excel Cell Coloring using xlsx
library(xlsx)
# To export the data
sheetname <- "mysheet"
write.xlsx(d, "mydata.xlsx", sheetName=sheetname)
file <- "mydata.xlsx"
# I want to color special letters in one cell
wb <- loadWorkbook(file) # load workbook
fo1 <- Fill(foregroundColor="blue") # create fill object # 1
cs1 <- CellStyle(wb, fill=fo1) # create cell style # 1
sheets <- getSheets(wb) # get all sheets
sheet <- sheets[[sheetname]] # get specific sheet
rows <- getRows(sheet, rowIndex=2:(nrow(df)+1)) # get rows
# 1st row is headers
cells <- getCells(rows, colIndex = 2:cols) # get cells
values <- lapply(cells, getCellValue) # extract the cell values
接下来,我们找到需要突出显示的字母
# find cells meeting conditional criteria D
highlightblue <- NULL
for (i in names(values)) {
x <- as.character(values[i])
if (x == D && !is.na(x)) {
highlightblue <- c(highlightblue, i)
}
}
应用格式并保存工作簿。
lapply(names(cells[highlightblue]),
function(ii) setCellStyle(cells[[ii]], cs1))
saveWorkbook(wb, file)
但是,我无法弄清楚如何为单元格中的字母着色。