I m building a shiny app with an interactive heatmap. I want to be able to select datacells in the heatmap in order to show more information about the samples. It seems that it only renders once tho. I replaced my actual data by runif(1) and the number is always the same! How can I get it to change each time I click on the heatmap?
This is the server for the plot, can the problem be the modular approach?
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)
I have tried observe events, making the variable reactive and also played arround with the session. Nothing works. I am expecting to see the runif(1) output change each time I click the heatmap,
First things first, you increase the likelihood of getting help if you provide aminimal yet reproducible example (I fixed htta here for you, that is adding the UI part and the shiny scaffold, idea is that we can simply copy paste your code and see the issue right away)
Now to your problem at hand. your
renderPrintfunction does not take any dependency on a reactive. That is it fired once, but it is not told to re-fire upon a click on the heatmap. The solution is easy though, simply add the dependency by claling your reactive: