Closed
Description
I tried to create a heatmap with R plotly in which the user can select a specific point ("block"). However, when the heatmap was based on a data.frame() then the clicked point information is only recorded if the first point in that row is not missing (z is not NA). For heatmaps based on a matrix() I don't experience any problem.
In the reproducible example: E.g. try to select the point with x = b and y = 1.
library(shiny)
library(plotly)
ui <- fluidPage(
fluidRow(column(6, h4("Heatmap based on data.frame()"),
plotlyOutput("plot1"),
h4("Selected point in heatmap"),
verbatimTextOutput("selected1")
),
column(6,
h4("Heatmap based on matrix()"),
plotlyOutput("plot2"),
h4("Selected point in heatmap"),
verbatimTextOutput("selected2")
)
)
)
server <- function(input, output) {
randomValues <- rnorm(9)
# Heatmap based on data.frame(): not working correctly, try to select x = b; y = 1
output$plot1 <- renderPlotly({
set.seed(1)
myData <- data.frame(x = rep(letters[1:3], times = 3),
y = rep(as.character(1:3), each = 3), value = randomValues)
myData$value[1] <- NA
plot_ly(
x = ~x, y = ~y, z = ~value, data = myData,
type = "heatmap", source = "plot1"
)
})
output$selected1 <- renderPrint(
event_data("plotly_click", source = "plot1")
)
# Heatmap based on matrix()
output$plot2 <- renderPlotly({
m <- matrix(randomValues, nrow = 3, ncol = 3, byrow = TRUE)
m[1,1] <- NA
plot_ly(
x = c("a", "b", "c"), y = as.character(1:3),
z = m, type = "heatmap", source = "plot2"
)
})
output$selected2 <- renderPrint(
event_data("plotly_click", source = "plot2")
)
}
shinyApp(ui = ui, server = server)