【问题标题】:Conditional Formatting Cell R DataTable条件格式单元格 R 数据表
【发布时间】:2017-06-05 21:35:18
【问题描述】:

我需要在我的数据表中实现条件格式。请参阅下面的示例。我需要的是根据该数字是否大于第 4 行和第 5 行中的阈值来突出显示第三行“PercentDone”。

如果数字大于 50%,我想将其突出显示为绿色。

如果它在 25% 和 50% 之间,我想用黄色突出显示它。

如果低于 25%,我想用红色突出显示。

这类似于有人在 Excel 中使用条件格式执行的操作,我只是不确定如何在 R 中的数据表中实现它。

在下面的示例中,第 1 列中的 46% 应为黄色,第 2 列中的 11% 应为红色,第 3 列中的 65% 应为绿色。

df = data.frame(
  c(51, 59, '46%', '25%', '50%'),
  c(12, 93, '11%', '25%', '50%'),
  c(40, 22, '65%', '25%', '50%'))

colnames(df) = c('Location1', 'Location2', 'Location3')
rownames(df) = c('Done', 'Need', 'PercentDone', 'Threshold1', 'Threshold2')

DT = datatable(df) %>%
  formatStyle(...)

【问题讨论】:

  • 阈值是每次都改变还是固定的?
  • 你的 df 每一列的混合格式(数字和字符)是故意的吗?
  • 感谢您的回复。是的,这是故意的,因为桌子必须这样看。我想获得第 3 行的数字版本可以由as.numeric(gsub('%', '', df[3,])) 完成。阈值是固定的,这确实使它更容易一些。我主要对在formatStyle() 中添加什么感到困惑。
  • 有谁知道实现这一目标的方法吗?

标签: r datatable


【解决方案1】:

回复晚了,但可能对其他人有用,所以我发帖。
如果转置表格没有问题,您可以尝试以下方法。

决赛桌应如下所示:

DF = data.frame(
 c(51, 59, '46%', '25%', '50%'),
 c(12, 93, '11%', '25%', '50%'),
 c(40, 22, '65%', '25%', '50%'), stringsAsFactors = FALSE )# variables as chr, w/o factor levels)


colnames(DF) = c('Location1', 'Location2', 'Location3')
rownames(DF) = c('Done', 'Need', 'PercentDone', 'Threshold1', 'Threshold2')

head(DF)

仅从百分比中检索数字,转换为数字以便能够执行比较:

# Define function for retrieving digits; One-liner courtesy of @stla at GitHub
Numextract <- function(string){ unlist(regmatches(string,gregexpr("[[:digit:]]+\\.*[[:digit:]]*",string)))}

# Apply Numextract to all dataframe; 
# retrieves only digits but still class is chr
DF [,] <- lapply(DF[,], Numextract)

# Convert to numeric to allow for comparison
DF [,] <- lapply(DF[,], as.numeric)

# Transpose dataframe to access the `PercentDone` as a column
DF = t(DF)

考虑从数据框中删除值并将其作为 vars

Threshold1 = 25
Threshold2 = 50

自定义数据表:突出显示PercentDone

DT::datatable(DF, 
          filter  = "bottom",
          caption = "I am the title",


          # OPTIONS:
          options = list(

            initComplete = JS(
              "function(settings, json) {",
              "$(this.api().table().header()).css({'background-color': '#000', 'color': '#fff'});",
              "}"),
            columnDefs = list(list(targets = length(colnames(DF)), visible = TRUE)))) %>% 

# Change fontsize of cell values
 formatStyle(columns    = c(1:length(colnames(df))), 
           fontSize   = "85%",
          fontFamily = "Verdana")%>%



 # Format column based on P.Value levels
 formatStyle(fontWeight = 'bold',
          # Format this:
          "PercentDone",

          # Font color
          color = styleInterval(c(0.0), c('black', 'black')),
          backgroundColor = styleInterval(c(Threshold1, Threshold2), 
                                          c('lightgreen', '#f4d35e', "tomato"))) -> fancyTable


# Print customized color-coded datatable:
fancyTable

【讨论】:

  • 我建议使用 StackOverflow 的内置系统上传图像,尤其是在您链接到其他人的保管箱时。如果链接失效。
  • 感谢您的回答。我已经及时离开了这个项目,但我希望这对未来的人有所帮助。再次感谢。
猜你喜欢
  • 1970-01-01
  • 2017-06-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-09
  • 2017-03-19
  • 2011-07-01
  • 2015-08-24
  • 1970-01-01
相关资源
最近更新 更多