简体   繁体   English

如何在 R Shiny 中显示来自 plotly_click 的许多点?

[英]How to display many points from plotly_click in R Shiny?

I have a plotly plot in R Shiny.我在 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.该图运行良好,我可以获得 1 个 plotly_click(通过 event_data())以显示在表格中。 How can a grow a vector of many event_data points.如何增长许多 event_data 点的向量。 Here is some sample code.这是一些示例代码。 I was trying to save the event in d_save.我试图将事件保存在 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).这不是纯 plotly 的一个坏例子(不使用 ggplot)。

I fixed it by:我通过以下方式修复它:

  • changing the d_save <- c(...) assignment to a d_save <<- c(...) (using a reactiveValues here would be cleaner).d_save <- c(...)赋值更改为d_save <<- c(...) (此处使用reactiveValues会更清晰)。
  • changing the plotly call to be a pipe, which seemingly allows some settings to carry over (like the type=scatter default) - eliminating the warning:将 plotly 调用更改为管道,这似乎允许保留某些设置(例如type=scatter默认值) - 消除警告:

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.修复了d_save分配中的“一对一”索引错误。
  • added a layout(...) to give it a title (this is useful for a lot of things).添加了一个layout(...)给它一个标题(这对很多事情都很有用)。

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:图像:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当已经具有 event_data(&quot;plotly_click&quot;) 时,如何使用 r、plolty、shin 为条形图中单击的条着色 - How to color a clicked bar from barchart with r, plolty, shiny when having already event_data("plotly_click") 在R Shiny中基于plotly_click在图表中显示数据 - Displaying data in the chart based on plotly_click in R shiny 将 plotly_click 应用于 shiny 应用程序中的 2 个以上图 - Apply plotly_click to more than 2 plots in a shiny app 在R和plotly中使用plotly_click更新多个信息框 - Updating multiple infobox using plotly_click in R and plotly 通过在 shiny 应用程序 r 中单击 plotly 来排除数据点 - Exclude data points by click in plotly in a shiny app r R Plotly 4.9.0 版中的 plotly_click 错误。 新版本有bug吗? - Errors with plotly_click in R Plotly version 4.9.0. Is there a bug in the new version? 使用flashBS包中的bsModal和plotly R plotly_click在弹出窗口中生成新的图 - Use bsModal in the shinyBS package with plotly R plotly_click to generate new plot in pop up 在 R Shiny 中显示 Plotly 图形 - Display Plotly graph in R Shiny 从Shiny,Plotly-R中提取所有点击事件图 - Extract all click event plots from Shiny, Plotly - R 如何使用R Shiny从可绘制的图形中显示表中的多行并删除表中的最后一行? - How to display multiple rows in the table from plotly graphs using R Shiny and delete the last row from the table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM