I am trying to add a feature in my Shiny app where a user can click a point on a scatterplot and it will be highlighted. This seems simple enough, but in my example below, I can't seem to get anything to be highlighted at all, no matter how many variations I try. I've looked through the advice in this post and this post, but they don't seem to help.
Any insight would be greatly appreciated!
# Define UI
ui <- fluidPage(
fluidRow(
column(6,
h3("Scatterplot"),
plotlyOutput("phylo_tree")),
)
)
# Define server logic
server <- function(input, output) {
# Phylogenetic Tree
output$phylo_tree <- renderPlotly({
# Create a scatter plot using plot_ly
scatter_plot <- plot_ly(data = mtcars, x = ~mpg, y = ~wt, type = "scatter", mode = "markers",
color = ~cyl, size = ~hp, text = ~paste("Car: ", rownames(mtcars)))%>%
# Highlight selected point
highlight(~mpg, on = "plotly_click",off = "plotly_doubleclick")
scatter_plot
})
}
# Run the application
shinyApp(ui, server)
I've tried including many variations of the arguments documented here, but I consistently get the same result: In the R Shiny app, nothing is highlighted when clicked.
This is not a problem with the shiny
framework, but with the plotly
code itself. Changing it to
scatter_plot <- mtcars |> highlight_key(~mpg) |>
plot_ly(x = ~mpg,y = ~wt,type = "scatter",mode = "markers",color = ~cyl,
size = ~hp,text = paste("Car: ",rownames(mtcars))) |>
highlight(on = "plotly_click",off = "plotly_doubleclick")
should do what you want.