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

为InteractiveComplexHeatmap Shiny应用添加绘图渲染选项的问题

解决方案

问题1:修复effectPlot无法渲染的问题

从Shiny+ggplot2的常见故障点入手,按以下步骤排查修复:

  1. 确认Shiny输出绑定正确
    在UI中添加plotOutput("effectPlot"),确保server端用renderPlot()对应输出,示例代码:

    # UI部分(根据布局需求放在对应位置)
    plotOutput("effectPlot", height = "300px")
    
    # Server部分
    output$effectPlot <- renderPlot({
      # 筛选选中基因和配对的数据
      selected_data <- subset(fc.df, gene == input$gene_select & pair == input$pair_select)
      
      # 处理无匹配数据的边界情况
      if(nrow(selected_data) == 0) {
        return(ggplot() + annotate("text", x = 1, y = 1, label = "无对应数据") + theme_void())
      }
      
      # 绘制目标图形
      ggplot(selected_data, aes(x = pair, y = effect.size)) +
        geom_bar(stat = "identity", fill = "#3498db") +
        geom_errorbar(
          aes(ymin = effect.size - effect.size.error, ymax = effect.size + effect.size.error),
          width = 0.2, color = "#2c3e50"
        ) +
        labs(title = paste(input$gene_select, "在", input$pair_select, "中的效应值"),
             y = "Effect Size", x = "配对样本组") +
        theme_minimal()
    })
    
  2. 检查数据与控件匹配

    • 确保selectInput的choices参数正确绑定了fc.df中的唯一基因/配对值(比如choices = unique(fc.df$gene))
    • 验证fc.df的列名完全匹配代码中的引用(gene、pair、effect.size、effect.size.error)
    • 确认已加载ggplot2包(server开头加library(ggplot2))

问题2:调整effectPlot的布局

方案A:显示在侧边栏选择列表下方

直接在sidebarPanel中,将plotOutput放在两个选择框之后:

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("gene_select", "选择基因:", choices = unique(fc.df$gene)),
      selectInput("pair_select", "选择配对:", choices = unique(fc.df$pair)),
      # 放置在选择框下方
      plotOutput("effectPlot", height = "300px")
    ),
    mainPanel(
      InteractiveComplexHeatmapOutput("heatmap")
    )
  )
)

方案B:显示在主面板子热图区域的右侧下方

利用Shiny的栅格布局(fluidRow+column)实现:

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("gene_select", "选择基因:", choices = unique(fc.df$gene)),
      selectInput("pair_select", "选择配对:", choices = unique(fc.df$pair))
    ),
    mainPanel(
      # 第一行放热图
      InteractiveComplexHeatmapOutput("heatmap"),
      # 第二行右侧放effectPlot
      fluidRow(
        column(8, ""), # 左侧占位,占8列宽度
        column(4, plotOutput("effectPlot", height = "300px")) # 右侧占4列
      )
    )
  )
)

如果希望热图和effectPlot在同一行(热图左、effectPlot右),可以调整为:

mainPanel(
  fluidRow(
    column(8, InteractiveComplexHeatmapOutput("heatmap")),
    column(4, plotOutput("effectPlot", height = "300px"))
  )
)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 05:27:24