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

Shiny棒球幻想联盟应用:分类动态排序与最值查询优化需求

解决Shiny棒球数据Top5筛选的排序适配问题

方案一:自动适配分类的排序逻辑

针对ERA和WHIP这类数值越低越优的指标,我们可以在排序逻辑中加入判断,自动切换排序方向:

修改server部分的排序代码,将原固定降序的arrange(-.data[[input$cat]])替换为动态判断逻辑:

server <- function(input, output) {
  x <- reactive({
    asc <- function(x) {
      xtfrm(x)
    }
    a <- input$cat
    b <- list("2022","2021","2020","2019","2018","2017","2016")
    c <- case_when(input$season == "Entire History" ~ b,
                   TRUE ~ list(input$season))
    d <- list("Allan","Alex","Carter","Derek","Eddie","Hammy","Jared","Josh","Niska","Ryan C","Ryan D","Urby")
    e <- case_when(input$team == "All" ~ d,
                   TRUE ~ list(input$team))
    f <- list("Normal","Long","Short")
    g <- case_when(input$weektype == "All" ~ f,
                   TRUE ~ list(input$weektype))
    e <- WeeklyLeagueData %>% 
          filter(.data[[input$cat]] != 0) %>%
          filter(Season %in% c) %>% 
          filter(Team %in% e) %>% 
          filter(WeekType %in% g) %>% 
          select(Team,input$cat,Season,Week,WeekType) %>% 
          # 新增判断:ERA/WHIP用升序,其余用降序
          arrange(if(input$cat %in% c("ERA", "WHIP")) .data[[input$cat]] else desc(.data[[input$cat]])) %>% 
          head(5)
    return(e)
  })
  
  output$table <- renderDataTable(
    {x()},
    options = list("searching"=FALSE,
                   "info"=FALSE,
                   "lengthChange"=FALSE,
                   "autoWidth"=TRUE,
                   "ordering"=FALSE,
                   "paging"=FALSE)
    )
}

选中ERA或WHIP时,会自动按升序取Top5(数值最低的5条记录),其余分类保持降序取最高值Top5。

方案二:新增用户控件自定义排序方向(推荐)

如果需要支持查看各队的最差表现(比如最高的ERA、最多的失误等),可以新增单选按钮让用户自主选择排序方向:

1. 修改UI部分,添加排序方向选择控件

在sidebarPanel中新增radioButtons:

ui <- fluidPage(
        titlePanel("PPBDL Records"),
        sidebarLayout(
          sidebarPanel(
            selectInput(
              inputId = "cat",
              label = "Category",
              choices = c("R","HR","RBI","SB","OBP","SLG","W","K","SV","HLD","ERA","WHIP"),
              selected = "R"),
            # 新增排序方向选择控件
            radioButtons(
              inputId = "sort_dir",
              label = "Query Type",
              choices = c("Highest Value (Descending)" = "desc", 
                          "Lowest Value (Ascending)" = "asc"),
              selected = "desc"),
            selectInput(
              inputId = "season",
              label = "Season",
              choices = c("Entire History","2022","2021","2020","2019","2018","2017","2016"),
              selected = "Entire History"),
            selectInput(
              inputId = "team",
              label = "Team",
              choices = c("All","Allan","Alex","Carter","Derek","Eddie","Hammy","Jared","Josh","Niska","Ryan C","Ryan D","Urby"),
              selected = "All"),
            selectInput(
              inputId = "weektype",
              label = "Week Type",
              choices = c("All","Normal","Long","Short"),
              selected = "All")
            ),
          mainPanel(
            fluidRow(
              column(5,
                dataTableOutput('table')
                    )
                  )
                )
              )
)              

2. 修改Server部分,根据用户选择执行排序

将排序逻辑改为根据input$sort_dir的值动态调整:

server <- function(input, output) {
  x <- reactive({
    asc <- function(x) {
      xtfrm(x)
    }
    a <- input$cat
    b <- list("2022","2021","2020","2019","2018","2017","2016")
    c <- case_when(input$season == "Entire History" ~ b,
                   TRUE ~ list(input$season))
    d <- list("Allan","Alex","Carter","Derek","Eddie","Hammy","Jared","Josh","Niska","Ryan C","Ryan D","Urby")
    e <- case_when(input$team == "All" ~ d,
                   TRUE ~ list(input$team))
    f <- list("Normal","Long","Short")
    g <- case_when(input$weektype == "All" ~ f,
                   TRUE ~ list(input$weektype))
    e <- WeeklyLeagueData %>% 
          filter(.data[[input$cat]] != 0) %>%
          filter(Season %in% c) %>% 
          filter(Team %in% e) %>% 
          filter(WeekType %in% g) %>% 
          select(Team,input$cat,Season,Week,WeekType) %>% 
          # 根据用户选择的方向排序
          arrange(if(input$sort_dir == "desc") desc(.data[[input$cat]]) else .data[[input$cat]]) %>% 
          head(5)
    return(e)
  })
  
  output$table <- renderDataTable(
    {x()},
    options = list("searching"=FALSE,
                   "info"=FALSE,
                   "lengthChange"=FALSE,
                   "autoWidth"=TRUE,
                   "ordering"=FALSE,
                   "paging"=FALSE)
    )
}

这个方案不仅解决了ERA/WHIP的排序问题,还让用户可以灵活查询每个分类的最高值或最低值Top5,满足查看最差表现的需求。

内容的提问来源于stack exchange,提问作者Clifford Applegarth

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 06:15:43