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

在Reactable详情面板嵌入bslib卡片时R Shiny报错求助

在reactable详情面板中嵌入bslib card的报错解决方案

问题背景

开发Shiny App时,尝试通过reactable的详情面板展示嵌套在bslib::card内的内容,触发如下模糊报错:

Warning: Error in FUN: 'options' must be a fully named list, or have no names (NULL)
  142: stop
  ...(省略中间调用栈)
   99: output$tbl
    1: runApp

最小可复现代码:

library(shiny)
library(bslib)
library(reactable)
library(htmltools)
library(htmlwidgets)

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

server <- function(input, output, sesion) {
  output$tbl <- renderReactable({
    reactable(
      iris,
      details = function(index) {
        card(card_body("Test"))
      }
    )
  })
}

shinyApp(ui, server)

报错原因

reactable的details函数要求返回可被JSON序列化的HTML内容,而bslib::card是包含Shiny依赖项(CSS/JS资源)的复杂组件,直接返回card对象会导致reactable在序列化依赖结构时出错,触发上述报错。

解决方案

需要将card的HTML标签与它的依赖项绑定后再返回,确保reactable能正确处理。使用htmltools::attachDependencies自动附加card的所有依赖是最通用的方法:

library(shiny)
library(bslib)
library(reactable)
library(htmltools)

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

server <- function(input, output, session) {
  output$tbl <- renderReactable({
    reactable(
      iris,
      details = function(index) {
        # 创建card组件
        card_element <- card(card_body(p("这是详情面板中的卡片内容")))
        # 将card的依赖附加到其HTML标签上
        attachDependencies(as.tags(card_element), card_element$dependencies)
      }
    )
  })
}

shinyApp(ui, server)

效果验证

上述代码可正常运行,点击reactable的展开按钮后,详情面板会显示带有正确bslib样式的卡片,无报错出现。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 20:08:25