R Shiny:如何使模块化情节反应?

问题描述 投票:0回答:1

我正在构建一个带有交互式热图的闪亮应用程序。我希望能够在热图中选择数据单元,以显示有关样本的更多信息。它似乎只渲染一次。我用 runif(1) 替换了实际数据,并且数字始终相同!每次点击热图时如何让它改变?

这是剧情的服务器,难道是模块化的问题?

library(shiny)
library(plotly)

GEA_ui <- function(id) {
  ns <- NS(id)
  fluidRow(
    column(
      width = 6,
      plotlyOutput(ns("interactiveHeatP"))      
    ),
    column(
      width = 6,
      verbatimTextOutput(ns("click")))
  )
}

GEA_server <- function(id){
  moduleServer(id, function(input, output, session) {
    output$interactiveHeatP <- renderPlotly({ 
      data <- as.matrix(mtcars)
      plot_ly(x = colnames(data), 
              y = rownames(data),
              z = data, 
              type = "heatmap", 
              source = "heatm") |>
        event_register("plotly_click")
    })

    points <- reactive({
      event_data("plotly_click", 
                 source = "heatm", 
                 priority = "event")
      })
     output$click <- renderPrint({
       print(runif(1))
    })
  })
}

ui <- fluidPage(
  GEA_ui("gea")
)

server <- function(input, output, session) {
  GEA_server("gea")
}

shinyApp(ui, server)

我尝试过观察事件,使变量具有反应性,并在会话中进行操作。什么都不起作用。我希望每次单击热图时都能看到 runif(1) 输出的变化,

r events shiny plotly heatmap
1个回答
0
投票

首先,如果您提供最小但可重现的示例(我在这里为您修复了 htta,即添加 UI 部分和闪亮的脚手架,想法是我们可以简单地复制粘贴您的代码,那么您会增加获得帮助的可能性并立即查看问题)


现在解决您手头的问题。您的

renderPrint
函数不会对响应式有 any 依赖。也就是说,它被触发了一次,但没有被告知在点击热图时重新触发。解决方案很简单,只需通过调用您的响应来添加依赖项即可:

output$click <- renderPrint({
  ## adding this will tell shiny to fire this render function 
  ## whenever there is a click
  points() 
  print(runif(1))
})

© www.soinside.com 2019 - 2024. All rights reserved.