Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# I'm trying to let the user select points and paint them in a certain colour, and if the user clicks on a point then paint that point a different colour.
# It looks like the pointNumber and curveNumber data that plotly returns are different for the same points. I'm not sure how curveNumber works, but to me it doesn't make sense yet :)
# Any help is appreciated!
library(plotly)
library(shiny)
ui <- fluidPage(
plotlyOutput("plot")
)
server <- function(input, output, session) {
output$plot <- renderPlotly({
click_data <- event_data("plotly_click", source = "select")
select_data <- event_data("plotly_selected", source = "select")
data <- mtcars
data$col <- "black"
if (!is.null(select_data)) {
cat(str(select_data))
idx <- select_data$pointNumber + 1
data[idx, "col"] <- "blue"
}
if (!is.null(click_data)) {
cat(str(click_data))
idx <- click_data$pointNumber + 1
data[idx, "col"] <- "red"
}
p <- ggplot(data, aes(mpg, wt, col = I(col))) + geom_point()
ggplotly(p, source = "select")
})
}
shinyApp(ui, server)
# This is very similar to the previous file, but I tried applying Carson's advise of setting the key aesthetic to row names
library(plotly)
library(shiny)
ui <- fluidPage(
plotlyOutput("plot")
)
server <- function(input, output, session) {
output$plot <- renderPlotly({
click_data <- event_data("plotly_click", source = "select")
select_data <- event_data("plotly_selected", source = "select")
data <- mtcars
key <- row.names(data)
data$col <- "black"
if (!is.null(select_data)) {
cat(str(select_data))
idx <- select_data$pointNumber + 1
data[idx, "col"] <- "blue"
}
if (!is.null(click_data)) {
cat(str(click_data))
idx <- click_data$pointNumber + 1
data[idx, "col"] <- "red"
}
p <- ggplot(data, aes(mpg, wt, col = I(col), key = key)) + geom_point()
ggplotly(p, source = "select")
})
}
shinyApp(ui, server)
@cpsievert
Copy link

cpsievert commented Sep 19, 2017

I know it seems counter-intuitive, but pointNumber isn't a reliable row identifier. Use a key variable like this:

library(plotly)
library(shiny)

mtcars$key <- row.names(mtcars)
mtcars$col <- "black"

ui <- fluidPage(
  plotlyOutput("plot")
)

server <- function(input, output, session) {
  output$plot <- renderPlotly({
    click_data <- event_data("plotly_click")
    select_data <- event_data("plotly_selected")
    if (!is.null(select_data)) {
      mtcars[mtcars$key %in% select_data$key, "col"] <- "blue"
    }
    if (!is.null(click_data)) {
      mtcars[mtcars$key %in% click_data$key, "col"] <- "red"
    }
    p <- ggplot(mtcars, aes(mpg, wt, col = I(col), key = key)) + 
      geom_point()
    ggplotly(p) %>% layout(dragmode = "lasso")
  })
}

shinyApp(ui, server)

@romanhaa
Copy link

romanhaa commented Mar 3, 2020

Hey guys! I'm having a similar problem: In my case I retrieve the event_data("plotly_selected") data outside of the renderPlotly() call and the only columns it contains are: curveNumber, pointNumber, x, y. All the columns from the plot data are missing (as well as the key column). Is this normal? Since pointNumber != row number (which is really confusing), I have to build my own identifier from the x-y coordinates, then do the same in the original plot data, and match the two. I imagine this not to be very efficient but I don't see any better way. I'll try to make an example later but perhaps it's already clear from my explanation.

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