3

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)

1 Answer 1

5

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:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.