You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shiny Reactable表格行级条件格式化:高亮指定列最小值

解决方案

要实现每行第3-6列中括号外数值的最小值高亮,我们可以通过reactable的自定义单元格渲染功能结合正则表达式提取数值来完成,以下是完整的Shiny应用代码:

library(shiny)
library(tidyverse)
library(reactable)

final_odds <- structure(list(player_prop = c("Aaron Jones: Rush + Rec Yards", 
                                             "Aaron Jones: Rush + Rec Yards", "Aaron Rodgers: Interceptions", 
                                             "Aaron Rodgers: Interceptions", "Aaron Rodgers: Pass TDs", "Aaron Rodgers: Pass TDs", 
                                             "Aaron Rodgers: Pass Yards", "Aaron Rodgers: Pass Yards", "Adam Thielen: Rec Yards", 
                                             "Adam Thielen: Rec Yards"), Side = c("Over", "Under", "Over", 
                                                                                  "Under", "Over", "Under", "Over", "Under", "Over", "Under"), 
                             DraftKings = c("1300.5 (-115)", "1300.5 (-115)", "7.5 (115)", 
                                            "7.5 (-140)", "31.5 (-120)", "31.5 (100)", "4050.5 (-110)", 
                                            "4050.5 (-110)", "750.5 (-110)", "750.5 (-120)"), BetMGM = c("-", 
                                                                                                         "-", "-", "-", "-", "-", "-", "-", "699.5 (-125)", "699.5 (-105)"
                                            ), FanDuel = c("-", "-", "-", "-", "30.5 (-112)", "30.5 (-112)", 
                                                           "3950.5 (-112)", "3950.5 (-112)", "750.5 (-112)", "750.5 (-112)"
                                            ), Caesars = c("-", "-", "-", "-", "30.5 (-115)", "30.5 (-115)", 
                                                           "4000.5 (-115)", "4000.5 (-115)", "775.5 (-115)", "775.5 (-115)"
                                            )), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
                                            ))

# 定义提取括号外数值的辅助函数
extract_value <- function(x) {
  # 提取字符串开头的数字(包含小数),空值"-"会转为NA
  as.numeric(str_extract(x, "^\\d+\\.?\\d*"))
}

ui <- fluidPage(
  reactableOutput("odds_table")
)

server <- function(input, output) {
  output$odds_table <- renderReactable({
    reactable(final_odds,
              columns = list(
                DraftKings = colDef(
                  render = function(value, index) {
                    # 获取当前行第3-6列的数值
                    row_vals <- map_dbl(final_odds[index, 3:6], extract_value)
                    # 计算该行最小值(忽略NA)
                    min_val <- min(row_vals, na.rm = TRUE)
                    current_val <- extract_value(value)
                    # 判断当前值是否为最小值,是则高亮绿色背景
                    if (!is.na(current_val) && current_val == min_val) {
                      div(style = list(backgroundColor = "#90EE90"), value)
                    } else {
                      value
                    }
                  }
                ),
                BetMGM = colDef(
                  render = function(value, index) {
                    row_vals <- map_dbl(final_odds[index, 3:6], extract_value)
                    min_val <- min(row_vals, na.rm = TRUE)
                    current_val <- extract_value(value)
                    if (!is.na(current_val) && current_val == min_val) {
                      div(style = list(backgroundColor = "#90EE90"), value)
                    } else {
                      value
                    }
                  }
                ),
                FanDuel = colDef(
                  render = function(value, index) {
                    row_vals <- map_dbl(final_odds[index, 3:6], extract_value)
                    min_val <- min(row_vals, na.rm = TRUE)
                    current_val <- extract_value(value)
                    if (!is.na(current_val) && current_val == min_val) {
                      div(style = list(backgroundColor = "#90EE90"), value)
                    } else {
                      value
                    }
                  }
                ),
                Caesars = colDef(
                  render = function(value, index) {
                    row_vals <- map_dbl(final_odds[index, 3:6], extract_value)
                    min_val <- min(row_vals, na.rm = TRUE)
                    current_val <- extract_value(value)
                    if (!is.na(current_val) && current_val == min_val) {
                      div(style = list(backgroundColor = "#90EE90"), value)
                    } else {
                      value
                    }
                  }
                )
              )
    )
  })
}

shinyApp(ui = ui, server = server)

关键细节说明

  1. 数值提取:extract_value函数使用正则表达式^\\d+\\.?\\d*提取字符串开头的数字(支持整数和小数),遇到"-"时会返回NA,完美处理空值场景。
  2. 行内最小值计算:在每个列的渲染函数中,通过map_dbl获取当前行所有目标列的数值,再用min(..., na.rm = TRUE)忽略空值计算最小值。
  3. 高亮逻辑:判断当前单元格的数值是否等于该行最小值且不为NA,若是则通过div标签设置浅绿色背景(#90EE90)。

内容的提问来源于stack exchange,提问作者Sam Hoppen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 02:33:29