-
-
Save daattali/9440f0b278dbbf538b3587e026811426 to your computer and use it in GitHub Desktop.
# 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) |
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.
Similar problem to romanhaa - does anybody have a neat solution for this? When using renderPlotly and not rendering a ggplot first, it doesn't seem trivial to keep track of rows efficiently. It's possible for example when encoding color with an integer, but the order gets thrown around within plotly if color is encoded as a string, or factor.
Is it possible to maintain the same zoom state after click/selection?
I know it seems counter-intuitive, but
pointNumber
isn't a reliable row identifier. Use a key variable like this: