2017-08-05 42 views
2

我想使用點擊事件中的數據獲取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 
}) 

關於如何將座標信息傳遞給情節圖的任何想法?

回答

2

我不知道如何使用nearPoints函數做到這一點,但是使用該函數真的有必要嗎?您可以在點擊點的閾值以及以下代碼中找到點:

library(shiny) 
library(plotly) 
library(DT) 

threshold_mpg = 3 
threshold_cyl = 1 

shinyApp(

    ui <- shinyUI(
    fluidPage(
     plotlyOutput("plot"), 
     DT::dataTableOutput("table") 
    ) 
), 
    function(input,output){ 

    data <- reactive({ 
     mtcars 
    }) 

    output$plot <- renderPlotly({ 
     d <- data() 
     plot_ly(d, x= ~mpg, y=~cyl, mode = "markers", type = "scatter", source="mysource") 
    }) 

    output$table<- DT::renderDataTable({ 

     event.data <- event_data("plotly_click", source = "mysource") 
     print(event.data) 
     if(is.null(event.data)) { return(NULL)} 

     # A simple alternative for the nearPoints function 
     result <- data()[abs(data()$mpg-event.data$x)<=threshold_mpg & abs(data()$cyl-event.data$y)<=threshold_cyl, ] 
     DT::datatable(result) 
    }) 
    } 
) 

希望這會有所幫助。

+0

優秀的建議@弗洛裏安。當我選擇圖上可用的點時,它運作良好。然而,當我點擊plot'cyl = 5'或'cyl = 7'上的某個地方時,它不會以相同的方式工作。 – Prradep

+1

@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

+0

我同意你的意見。我在閱讀這裏提到的帖子後發佈了這個問題。我會嘗試推測的方法。 – Prradep

1

"plotly_selected" plotly.js event返回的信息比event_data("plotly_selected")實際給出的信息要多,包括座標信息(這可以說是event_data()所做的設計錯誤,現在已經太遲了)。幸運的是,如果你知道一些JavaScript,懂得listen to plotly select events,以及如何send data from client to a shiny server,你可以做這樣的事情來訪問這些信息:

library(shiny) 
library(plotly) 
library(htmlwidgets) 

ui <- fluidPage(
    plotlyOutput("p"), 
    verbatimTextOutput("info") 
) 

server <- function(input, output, session, ...) { 

    output$p <- renderPlotly({ 
    plot_ly(x = 1:10, y = 1:10) %>% 
     layout(dragmode = "select") %>% 
     onRender(
     "function(el, x) { 
     var gd = document.getElementById(el.id); 
     gd.on('plotly_selected', function(d) { 
      // beware, sometimes this event fires objects that can't be seralized 
      console.log(d); 
      Shiny.onInputChange('my-select-event', d.range) 
     }) 
     }") 
    }) 


    output$info <- renderPrint({ 
    print(session$rootScope()$input[["my-select-event"]]) 
    }) 

} 

shinyApp(ui, server) 

使用的座標信息,你可以寫在工作的功能與nearPoints()類似的方式。

相關問題