我可以获取以下信息:“悬停位置”,“刷子位置”或“点击位置”

时间:2015-07-23 16:08:25

标签: r shiny plotly

我想设置一个有光泽和情节的交互式图表。 Shiny具有内置功能,可以获取有关用户交互的信息。例如:输入$ plot_click 输入$ plot_dblclick 输入$ plot_hover 输入$ plot_brush 。 看到: http://shiny.rstudio.com/articles/plot-interaction.html

有没有选择通过Plotly API获取此功能?或者API可以只处理一个方向吗?

Plotly非常酷。我喜欢在我的闪亮应用程序中使用它。

谢谢和最好的问候

尼科

2 个答案:

答案 0 :(得分:3)

是的,通过postMessage API有点击和悬停绑定到Plotly图: https://github.com/plotly/postMessage-API

如何在Shiny中使用postMessage API的草图如下: http://moderndata.plot.ly/dashboards-in-r-with-shiny-plotly/

代码在这里: https://github.com/chriddyp/plotly-shiny

答案 1 :(得分:1)

现在event_data包中有一个plotly函数可以处理悬停,点击,刷牙等。

包中有一个描述用法的Shiny演示:

library(plotly)
shiny::runApp(system.file("examples", "plotlyEvents", package = "plotly"))

<强> app.R

library(shiny)
library(plotly)

ui <- fluidPage(
  radioButtons("plotType", "Plot Type:", choices = c("ggplotly", "plotly")),
  plotlyOutput("plot"),
  verbatimTextOutput("hover"),
  verbatimTextOutput("click"),
  verbatimTextOutput("brush"),
  verbatimTextOutput("zoom")
)

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

  output$plot <- renderPlotly({
    # use the key aesthetic/argument to help uniquely identify selected observations
    key <- row.names(mtcars)
    if (identical(input$plotType, "ggplotly")) {
      p <- ggplot(mtcars, aes(x = mpg, y = wt, colour = factor(vs), key = key)) + 
        geom_point()
      ggplotly(p) %>% layout(dragmode = "select")
    } else {
      plot_ly(mtcars, x = mpg, y = wt, key = key, mode = "markers") %>%
        layout(dragmode = "select")
    }
  })

  output$hover <- renderPrint({
    d <- event_data("plotly_hover")
    if (is.null(d)) "Hover events appear here (unhover to clear)" else d
  })

  output$click <- renderPrint({
    d <- event_data("plotly_click")
    if (is.null(d)) "Click events appear here (double-click to clear)" else d
  })

  output$brush <- renderPrint({
    d <- event_data("plotly_selected")
    if (is.null(d)) "Click and drag events (i.e., select/lasso) appear here (double-click to clear)" else d
  })

  output$zoom <- renderPrint({
    d <- event_data("plotly_relayout")
    if (is.null(d)) "Relayout (i.e., zoom) events appear here" else d
  })

}

shinyApp(ui, server, options = list(display.mode = "showcase"))

他们还有关于主要GitHub页面的链接刷新和点击的示例:

https://github.com/ropensci/plotly