Skip to content

R plotly_click for heatmap not always working (due to NA) #1141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mvarewyck opened this issue Oct 27, 2017 · 1 comment
Closed

R plotly_click for heatmap not always working (due to NA) #1141

mvarewyck opened this issue Oct 27, 2017 · 1 comment

Comments

@mvarewyck
Copy link

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)
@cpsievert
Copy link
Collaborator

cpsievert commented Nov 1, 2018

Thanks for the report. Looks like event_data() is generally broken when x/y/z are all vectors of the same length (notice how z isn't reported in the cells that "work"). Turns out the pointNumber emitted by plotly.js refers to a relevant cell of the heatmap (i.e., it emits the relevant row and column index), but plotly isn't currently accounting for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants