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

R Shiny中第三个shinydashboard::box内plotly图表显示为空问题

问题解决:Shiny模块中Plotly图表无法显示的修复

核心问题

你在动态生成的box中使用了uiOutput来调用由renderPlotly生成的输出,这是错误的——renderPlotly对应的UI输出控件应该是plotlyOutput,而非uiOutput,这直接导致图表无法渲染。

修正后的Server代码

将final_chart的renderUI部分中的uiOutput替换为plotlyOutput,同时确保命名空间正确:

output$barcharting <- plotly::renderPlotly(fig)

output$final_chart <- shiny::renderUI({
  shinydashboard::box(
    title = "图表",
    status = 'primary',
    solidHeader = TRUE,
    collapsible = TRUE,
    collapsed = FALSE,
    width = 12,
    plotly::plotlyOutput(session$ns("barcharting")) # 替换uiOutput为plotlyOutput
  )
})

完整模块示例(含Server)

确保你的模块Server使用moduleServer规范编写,避免作用域和命名空间问题:

shiny_modServer <- function(id) {
  moduleServer(id, function(input, output, session) {
    # 示例:生成plotly图表fig(替换为你的实际代码)
    fig <- plotly::plot_ly(data = mtcars, x = ~mpg, y = ~wt, type = 'bar')
    
    output$barcharting <- plotly::renderPlotly(fig)
    
    output$final_chart <- shiny::renderUI({
      shinydashboard::box(
        title = "图表",
        status = 'primary',
        solidHeader = TRUE,
        collapsible = TRUE,
        collapsed = FALSE,
        width = 12,
        plotly::plotlyOutput(session$ns("barcharting"))
      )
    })
    
    # 补充ui_1和ui_2的渲染逻辑(根据你的实际需求编写)
    output$ui_1 <- shiny::renderUI({
      shiny::fluidRow(shiny::h3("模块UI1"))
    })
    
    output$ui_2 <- shiny::renderUI({
      shiny::h3("模块UI2")
    })
  })
}

主App调用示例

确保在主App中正确调用模块:

shinyApp(
  ui = shiny_modUI("test_module"),
  server = function(input, output, session) {
    shiny_modServer("test_module")
  }
)

额外检查点

  1. 确认fig在模块Server的作用域内生成,或已正确传递到模块中,避免作用域导致的对象不可用问题。
  2. 验证模块的命名空间在UI和Server中保持一致,所有输出ID都通过ns()或session$ns()正确包裹。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 06:02:49