4

我想建立一个有光泽和情节的交互式图表。Shiny 有一个内置功能来获取有关用户交互的信息。比如:input$plot_clickinput$plot_dblclickinput$plot_hoverinput$plot_brush。见: http ://shiny.rstudio.com/articles/plot-interaction.html

是否有任何选项可以通过 Plotly API 获得这个?或者 API 可以只处理一个方向吗?

Plotly 真的很酷。很想在我闪亮的应用程序中使用它。

谢谢和最好的问候

尼科

4

2 回答 2

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

于 2015-07-23T16:56:53.747 回答
2

现在有一个event_data来自plotly包本身的函数来处理悬停、点击、刷等。

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

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

应用程序.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

于 2016-05-23T14:16:16.763 回答