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

Shiny应用报错:无法在响应式消费者外访问响应式值求助

问题排查:Shiny中InteractiveComplexHeatmap筛选功能报错

错误原因

核心问题是observeEvent的语法使用错误:

  • observeEvent的第一个参数eventExpr需要是反应式值的向量/列表,用于指定触发回调的依赖项
  • 原代码中错误地将input$positive.cutoff作为第二个参数传入(本该是回调函数handlerExpr的位置),导致input$positive.cutoff在非反应性上下文被直接访问,触发了Shiny的反应性规则错误:"Can't access reactive value outside of reactive consumer"

修正后的代码

suppressPackageStartupMessages(library(InteractiveComplexHeatmap))
suppressPackageStartupMessages(library(ComplexHeatmap))
suppressPackageStartupMessages(library(ggplot2))


server <- function(input, output, session)
{
  set.seed(1)
  mat <- matrix(rnorm(10000,0,1),nrow = 1000, ncol = 10,dimnames = list(paste0("gene",1:1000),paste0("pair",1:10)))
  
  output$negative.cutoff <- renderUI({
    sliderInput("negative.cutoff","Set negative cutoff",min=min(mat),max=0,value=0)
  })
  
  output$positive.cutoff <- renderUI({
    sliderInput("positive.cutoff","Set positive cutoff",min=0,max=max(mat),value=0)
  })
  
  # 修正:将两个依赖项放在同一个向量中传入eventExpr
  observeEvent(c(input$negative.cutoff, input$positive.cutoff), {
    hm <- mat
    if(!is.null(input$negative.cutoff)){
      idx <- which(apply(hm,1,function(x) all(x <= input$negative.cutoff)))
      if(length(idx) > 0) hm <- hm[-idx,]
    }
    if(!is.null(input$positive.cutoff)){
      idx <- which(apply(hm,1,function(x) all(x >= input$positive.cutoff)))
      if(length(idx) > 0) hm <- hm[-idx,]
    }
    if(nrow(hm) > 0) {  # 修正:用nrow判断是否有剩余行,原!is.null(hm)始终为TRUE
      hm <- Heatmap(hm,show_row_names=F)
      hm <- draw(hm)
      makeInteractiveComplexHeatmap(input, output, session, hm, "heatmap")
    } else {
      output$heatmap <- renderPlot({
        grid.newpage()
        grid.text("Filtering removed all rows")
      })
    }
  }, ignoreNULL = FALSE)
}

ui <- fluidPage(
  titlePanel("Paralog pair KO Effects Explorer",windowTitle="Paralog pair KO Effects Explorer"),
  sidebarLayout(
    sidebarPanel(
      tags$head(
        tags$style(HTML(".multicol {-webkit-column-count: 3; /* Chrome, Safari, Opera */-moz-column-count: 3; /* Firefox */column-count: 3;}")),
        tags$style(type="text/css", "#loadmessage {position: fixed;top: 0px;left: 0px;width: 100%;padding: 5px 0px 5px 0px;text-align: center;font-weight: bold;font-size: 100%;color: #000000;background-color: #CCFF66;z-index: 105;}"),
        tags$style(type="text/css",".shiny-output-error { visibility: hidden; }",".shiny-output-error:before { visibility: hidden; }")),
      conditionalPanel(condition="$('html').hasClass('shiny-busy')",tags$div("In Progress...",id="loadmessage")),
      uiOutput("negative.cutoff"),
      uiOutput("positive.cutoff")
    ),
    mainPanel(
      InteractiveComplexHeatmapOutput(heatmap_id="heatmap",title1="heatmap",title3="cell info",
                                      width1=800,height1=800,width3=400,height3=300,
                                      action="click",response="click",output_ui_float=F)
    )
  )
)

shinyApp(ui, server)

额外修正说明:

  • 将!is.null(hm)改为nrow(hm) > 0,因为即使所有行被过滤,hm仍是一个0行的矩阵,不会为NULL,原判断逻辑无效
  • 统一使用<-赋值(原代码中output$heatmap = renderPlot用了=,Shiny中推荐用<-保持一致性)

内容的提问来源于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:55:00