我有下面的代码,其中我希望列Score
根据列max
的值进行条件格式化。如果得分为8,最大值为8,则为绿色。如果得分为4,最大值为8,则为黄色。如果得分为4,最大值为4,则为绿色。如果得分为2,最大值为4,则为黄色。如果得分为0,则为红色。但是,下面的程序似乎不起作用(仅测试绿色)。
根据G5W建议编辑代码
因此,使用下面的代码时,我没有得到条件格式,但也得到了错误消息:
,然后是
单击指向日志文件的链接,将显示以下xml输出
library(tibble)
library(openxlsx)
data <- tribble(
~Week, ~Facility, ~Indicator, ~`Indicator Value`, ~`Yellow Gap`, ~`Green Gap`, ~Score, ~max,
8, "Mngeta Health Center", "3MMD Coverage", 0.96, -13, 10, 4, 8,
8, "Mngeta Health Center", "12 Month Retention", 0.96, 35, 50, 2, 4,
8, "Mngeta Health Center", "Appointment Adherence", 0.97, 11, 24, 0, 8,
8, "Mngeta Health Center", "EID 12 Months", 1, 0, 0, 8, 8,
8, "Mngeta Health Center", "Early Retention", 1, 0, 0, 8, 8,
8, "Mngeta Health Center", "Recent Retention", 1.04, -19, -5, 8, 8,
8, "Mngeta Health Center", "6MMD Coverage", 0.98, -29, -9, 8, 8,
8, "Mngeta Health Center", "IPT Coverage", 0.99, -15, -1, 4, 4,
8, "Mngeta Health Center", "EID 2 Months", 1, 0, 0, 8, 8,
8, "Mngeta Health Center", "Viral Load Coverage", 0.95, -67, -2, 8, 8
)
# Convert Score column to numeric
data$Score <- as.numeric(data$Score)
wb <- createWorkbook()
# Add a new worksheet
addWorksheet(wb, "Formatted Data")
# Write the data to the worksheet
writeData(wb, "Formatted Data", data)
# Create a style object for green color
green_style <- createStyle(bgFill = "green")
conditionalFormatting(wb, sheet = "Formatted Data",
cols = which(colnames(data) == "Score"), rows = 1:(nrow(data)+1),
rule = '"Score"==8 & "max"==8', style = green_style)
saveWorkbook(wb, "formatted_data.xlsx", overwrite = TRUE)
由reprex package(v2.0.1)于2023年2月25日创建
1条答案
按热度按时间wfveoks01#
有两个问题。第一,如果要使用列名,则必须在Excel中用引号将其括起来。第二,由于您有标题行,因此要测试的行最多为nrow(data)+1。请尝试: