Can I get information such as “hover location”, “brush location” or “click location”,

I want to create an interactive schedule with a brilliant and plot. Shiny has a built-in feature for getting user interaction information. For example: enter $ plot_click, enter $ plot_dblclick, enter $ plot_hover and enter $ plot_brush. See: http://shiny.rstudio.com/articles/plot-interaction.html

Is there any way to get this through the Plotly API? Or can an API just handle one direction?

This is actually cool. I would like to use it in my brilliant applications.

Thanks and best regards

Niko

+4
source share
2 answers

event_data plotly, , , ..

, :

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

+1

Source: https://habr.com/ru/post/1599357/


All Articles