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

如何在选中reactable表格行时显示ConditionalPanel?

问题解决:Reactable选中行后显示DT表格

问题原因

原代码中conditionalPanel的条件逻辑错误:当reactable设置为单选模式(selection = "single")时,input.dt_mtcars_selected返回的是单个选中行的索引值(而非数组),原条件里的input.dt_mtcars_selected.length > 0会失效——因为数值类型没有length属性,导致条件始终不成立。

修改后的代码

library(shiny)
library(reactable)
library(DT)

server <- function(input, output) {

  # Reactable 数据表格
  output$dt_mtcars <- renderReactable({
    mtcars %>% 
      reactable(
        selection = "single",
        defaultSelected = c(),
        onClick = "select"
      )
  })

  # DT 数据表格
  output$dt_iris <- renderDataTable({
    iris
  })

}

ui <- fluidPage(
  # 修改条件:只需判断选中值不为null即可
  conditionalPanel(
    condition = "input.dt_mtcars_selected !== null",
    dataTableOutput("dt_iris")
  ),
  
  reactableOutput("dt_mtcars")
)

shinyApp(ui, server)

补充说明

  • 若后续改为多选模式(selection = "multiple"),则input.dt_mtcars_selected会返回数组,此时原条件input.dt_mtcars_selected !== null && input.dt_mtcars_selected.length > 0才适用。
  • 单选模式下,选中行时input.dt_mtcars_selected会返回对应行的索引(从1开始),未选中时为null,因此只需判断是否不为null就能控制表格的显示/隐藏。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 15:03:10