I try to implement a visual analog scale, where the user clicks on a line indicating her sensation.
I know how to do this in default graphics, but for other reasons I want to use plotly here. In the example below, this error message is shown:
The 'plotly_click' event tied a source ID of 'vas' is not registered. In order to obtain this event data, please add
event_register(p, 'plotly_click')to the plot (p) that you wish to obtain event data from.
(Correction: see my reply below)
How do I register the mouse event? There is almost no documentation left after plotly does no longer support R directly.
library(shiny)
library(plotly)
ui = fluidPage(
plotlyOutput("vas")
)
server = function(input, output, session) {
eventClick = reactive(event_data("plotly_click", source = "vas"))
observeEvent(eventClick(), {
# In the end, I need the x-coordinate here
print("click")
})
output$vas = renderPlotly({
plot_ly() |>
add_trace(
type = "scatter",
mode = "line",
x = c(0,10),
y = c(0,0)
) |>
plotly::layout(
xaxis = list(showgrid = FALSE, zeroline = TRUE,
showspikes = TRUE, spikesnap = "cursor"),
yaxis = list(visible = FALSE)
) |>
config(displayModeBar = FALSE) |>
event_register("plotly_click")
})
}
shinyApp(ui, server)