使用xlsx进行Excel单元格着色

ala*_*lap 7 excel r xlsx

初始代码:

让我们假设我们使用此命令来创建虚拟数据:

Data <- data.frame(
    X = paste(c(sample(1:10),sample(1:10)), collapse=";"),
    Y = sample(c("yes", "no"), 10, replace = TRUE)
)
Run Code Online (Sandbox Code Playgroud)

输出:

                                           X   Y
1  10;7;4;3;8;6;5;2;9;1;3;5;10;2;9;6;8;4;1;7 yes
2  10;7;4;3;8;6;5;2;9;1;3;5;10;2;9;6;8;4;1;7  no
3  10;7;4;3;8;6;5;2;9;1;3;5;10;2;9;6;8;4;1;7  no
4  10;7;4;3;8;6;5;2;9;1;3;5;10;2;9;6;8;4;1;7 yes
5  10;7;4;3;8;6;5;2;9;1;3;5;10;2;9;6;8;4;1;7  no
6  10;7;4;3;8;6;5;2;9;1;3;5;10;2;9;6;8;4;1;7 yes
7  10;7;4;3;8;6;5;2;9;1;3;5;10;2;9;6;8;4;1;7  no
8  10;7;4;3;8;6;5;2;9;1;3;5;10;2;9;6;8;4;1;7 yes
9  10;7;4;3;8;6;5;2;9;1;3;5;10;2;9;6;8;4;1;7 yes
10 10;7;4;3;8;6;5;2;9;1;3;5;10;2;9;6;8;4;1;7 yes
Run Code Online (Sandbox Code Playgroud)

题:

使用xlsx包我可以将X列数据输出到彩色的excel文件.

有没有办法我可以颜色让我们说值大于5红色,然后小于5蓝色,并放在同一个单元格的一切.基本上我只是把这个表写成excel但是有些值是彩色的.

先感谢您,

Jot*_*ota 15

我本质上是从这个问题和我的答案中复制代码 对此用例进行一些调整.我不确定这个礼节,但我只是想表明这可以做到!任何人,让我知道,如果我做了一些我不应该做的事情,重复使用链接问题中的代码来回答这个问题.如果现在另一个问题得到解答,这被视为重复,我对此很好.只是想帮忙!

首先,重新格式化数据.

# split the X column so there will be one numeric entry per cell 
d <- matrix(as.numeric(unlist(strsplit(as.character(Data$X), ";"))), 
           ncol = 20, byrow = TRUE)

d <- data.frame(d, Data$Y)
cols <- length(d[1, ]) # number of columns, we'll use this later
Run Code Online (Sandbox Code Playgroud)

其次,我们可以使用函数xlsx创建工作簿,然后获取单元格值.

library(xlsx)

# exporting data.frame to excel is easy with xlsx package
sheetname <- "mysheet"
write.xlsx(d, "mydata.xlsx", sheetName=sheetname)
file <- "mydata.xlsx"
# but we want to highlight cells if value greater than or equal to 5
wb <- loadWorkbook(file)              # load workbook
fo1 <- Fill(foregroundColor="blue")   # create fill object # 1
cs1 <- CellStyle(wb, fill=fo1)        # create cell style # 1
fo2 <- Fill(foregroundColor="red")    # create fill object # 2
cs2 <- CellStyle(wb, fill=fo2)        # create cell style # 2 
sheets <- getSheets(wb)               # get all sheets
sheet <- sheets[[sheetname]]          # get specific sheet
rows <- getRows(sheet, rowIndex=2:(nrow(d)+1))     # get rows
                                                   # 1st row is headers
cells <- getCells(rows, colIndex = 2:cols)         # get cells

# in the wb I import with loadWorkbook, numeric data starts in column 2
# The first column is row numbers.  The last column is "yes" and "no" entries, so
# we do not include them, thus we use colIndex = 2:cols

values <- lapply(cells, getCellValue) # extract the cell values
Run Code Online (Sandbox Code Playgroud)

接下来,我们找到需要根据标准格式化的单元格.

# find cells meeting conditional criteria > 5
highlightblue <- NULL
for (i in names(values)) {
  x <- as.numeric(values[i])
  if (x > 5 && !is.na(x)) {
    highlightblue <- c(highlightblue, i)
  }    
}

# find cells meeting conditional criteria < 5
highlightred <- NULL
for (i in names(values)) {
  x <- as.numeric(values[i])
  if (x < 5 && !is.na(x)) {
    highlightred <- c(highlightred, i)
  }    
}
Run Code Online (Sandbox Code Playgroud)

最后,应用格式并保存工作簿.

lapply(names(cells[highlightblue]),
       function(ii) setCellStyle(cells[[ii]], cs1))

lapply(names(cells[highlightred]),
       function(ii) setCellStyle(cells[[ii]], cs2))

saveWorkbook(wb, file)
Run Code Online (Sandbox Code Playgroud)

  • 我对条件格式不感兴趣,我只想给一些东西上色。不过还是谢谢大家的解答! (2认同)

Car*_*oft 0

我怀疑是否可以直接从R. 因此,首先打开 Excel 工作簿并将“X”列的条件格式设置为您想要的任何颜色与值条件。然后,当您从 R 写入工作簿时,就会发生着色。