The coordinates from event_data
are rounded, so how can I be guaranteed to identify the selected data? For example, if the two center points are selected, event_data
returns the same x value:
library(shiny)
library(plotly)
library(ggplot2)
ui = fluidPage(
# Plot
plotlyOutput("plot"),
# Table for x y coordinates
tableOutput("text")
)
# Create plot
dataDf = data.frame(x = c(-10, 1.004, 1.005, 10),
y = c(-10, 0, 0, 10))
plt = ggplot(dataDf, aes(x = x, y = y)) + geom_point()
ply = ggplotly(plt, source = "plot")
server = function(input, output) {
# Display plot
output$plot = renderPlotly(ply)
# Show x and y values of selected points in a table
output$text = renderTable({
eventData = event_data("plotly_selected", source = "plot")
data.frame(x = eventData$x, y = eventData$y)
})
}
shinyApp(ui, server)
You can set the desired number of digits in renderTable
:
output$text = renderTable({
eventData = event_data("plotly_selected", source = "plot")
print(eventData)
data.frame(x = eventData$x, y = eventData$y)
}, digits = 6)