2017-08-05 40 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()类似的方式。

相关问题