我想使用點擊事件中的數據獲取nearPoints
。在renderPlotly單擊事件中使用nearPoints時出現閃爍錯誤
我發現了Shiny webpage下面的代碼片段,它可以正常工作。
output$plot <- renderPlot({
d <- data()
plot(d$speed, d$dist)
})
output$plot_clickedpoints <- renderPrint({
# For base graphics, we need to specify columns, though for ggplot2,
# it's usually not necessary.
res <- nearPoints(data(), input$plot_click, "speed", "dist")
if (nrow(res) == 0)
return()
res
})
我試圖模仿的做法上面的識別使用點擊事件數據的Plotly地塊nearPoints
。但是,它沒有工作。
output$plot <- renderPlotly({
d <- data()
plot(d$speed, d$dist)
})
output$plot_clickedpoints <- renderPrint({
# For base graphics, we need to specify columns, though for ggplot2,
# it's usually not necessary.
res <- nearPoints(data(), event_data("plotly_click"), "speed", "dist")
if (nrow(res) == 0)
return()
res
})
關於如何將座標信息傳遞給情節圖的任何想法?
優秀的建議@弗洛裏安。當我選擇圖上可用的點時,它運作良好。然而,當我點擊plot'cyl = 5'或'cyl = 7'上的某個地方時,它不會以相同的方式工作。 – Prradep
@Prradep,恐怕解決這個問題超出了我的能力,因爲我對JavaScript的知識有限。我首先想到使用plotly_hover解決方案,並使用shinyjs的onclick()單擊時獲取座標,但是plotly_hover與plotly_click具有相同的問題。我懷疑甚至有可能從圖中的一個隨機點獲取座標,參見[這裏](https://community.plot.ly/t/how-can-i-get-exact-xy-pixel-coordinates -of-A-標記點/ 3009)。但是,從我的角度來看,這有更多的猜測,還有其他人可以更好地解決這個問題(如卡森)。 – Florian
我同意你的意見。我在閱讀這裏提到的帖子後發佈了這個問題。我會嘗試推測的方法。 – Prradep