简体   繁体   中英

How to display many points from plotly_click in R Shiny?

I have a plotly plot in R Shiny. I want to be able to click many points and have them displayed in a table. The plot is working great and I can get 1 plotly_click (via event_data()) to show in a table. How can a grow a vector of many event_data points. Here is some sample code. I was trying to save the event in d_save. Thanks.

library(shiny)
library(plotly)

data1 <- data.frame(cbind(seq(1,1000,1),seq(1,1000,1)*5))
colnames(data1) <- c('index','data')
data_points <- data.frame(cbind(seq(1,1000,5),seq(1,1000,5)*5))
colnames(data_points) <- c('index','data')


ui <- fluidPage(
  plotlyOutput("plot1"),
  tableOutput("dataTable")
)

d_save <- vector()

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

  # make plotly plot
  output$plot1 <- renderPlotly({
    p <- plot_ly(data1, x = data1$index, y = data1$data,mode = "lines")
    add_trace(p, x = data_points$index, y = data_points$data, mode = "markers")
  })

    # show table of stances 
    output$dataTable <- renderTable({
      d <- event_data("plotly_click")
      d_save <- c(d_save,d$pointNumber[2]+1)
      data.frame(d_save)
    })
}

shinyApp(ui, server)

There is nothing seriously wrong with this and it was weird that it never got answered. It is not a bad example of pure plotly (without using ggplot).

I fixed it by:

  • changing the d_save <- c(...) assignment to a d_save <<- c(...) (using a reactiveValues here would be cleaner).
  • changing the plotly call to be a pipe, which seemingly allows some settings to carry over (like the type=scatter default) - eliminating the warning:

No trace type specified: Based on info supplied, a 'scatter' trace seems appropriate.

  • fixed an "off-by-one" indexing error in the d_save assignment.
  • added a layout(...) to give it a title (this is useful for a lot of things).

The resulting code:

library(shiny)
library(plotly)

data1 <- data.frame(cbind(seq(1,1000,1),seq(1,1000,1)*5))
colnames(data1) <- c('index','data')
data_points <- data.frame(cbind(seq(1,1000,5),seq(1,1000,5)*5))
colnames(data_points) <- c('index','data')

ui <- fluidPage(
  plotlyOutput("plot1"),
  tableOutput("dataTable")
)

d_save <- vector()

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

  # make plotly plot
  output$plot1 <- renderPlotly({
    plot_ly(data1, x=data1$index, y=data1$data,mode = "lines") %>%
         add_trace(x = data_points$index, y=data_points$data, mode = "markers") %>%
         layout(title="Plotly_click Test")
  })

  # show table of point markers clicked on by number
  output$dataTable <- renderTable({
    d <- event_data("plotly_click")
    d_save <<- c(d_save,d$pointNumber[1]+1)
    data.frame(d_save)
  })
}
shinyApp(ui, server)

The image:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM