从 shiny R 中的多个散点图选择生成多个表

generate multiple tables from multiple selections of scatterplot in shiny R

我在闪亮的仪表板上有一个散点图,我想通过散点图的 selecting/highlighting 个不同区域生成 两个 个不同的 table。我目前能够通过 selecting/highlighting 一个区域生成一个 table,但是我不确定如何使它对两个 tables/selections 起作用(或者如果可能的话)。

如有任何帮助或建议,我们将不胜感激。谢谢

下面提供了用于生成带有散点图和 highlight/generate 单个 table 的闪亮仪表板的示例代码(取自 here

更多细节:理想情况下,此过程将通过手动 selecting/dragging 一些点上的区域来实现,生成第一个 table,然后手动 selecting/dragging 不同点子集上的区域并生成第二个 table。此后,如果选择了另一个区域,它将重置第一个选择和 table,然后下一个选择将重置第二个选择和 table。

ui <- fluidPage(

  plotOutput("plot", brush = "plot_brush"),
  tableOutput("data")
)
server <- function(input, output, session) {
  output$plot <- renderPlot({
    ggplot(mtcars, aes(wt, mpg)) + geom_point()
  }, res = 96)
  
  output$data <- renderTable({
    brushedPoints(mtcars, input$plot_brush)
  })
}
shinyApp(ui=ui, server=server)

也许这会有所帮助。您可以跟踪 reactiveValues 中的哪个 table(1 或 2)以及每个 table 的数据。让我知道这是否是您想要的。如果您想在绘图中保留之前的选择,我认为您可能需要手动放置一个矩形。 A github issue allowing for multiple selections of brushed points is an open issue (enhancement). Alternatively, you could tag points for each table based on this approach.

library(shiny)

ui <- fluidPage(
  plotOutput("plot", brush = "plot_brush"),
  h2("Table 1"),
  tableOutput("data1"),
  h2("Table 2"),
  tableOutput("data2")
)

server <- function(input, output, session) {
  
  rv <- reactiveValues(table = 1,
                       data1 = NULL,
                       data2 = NULL)
  
  output$plot <- renderPlot({
    ggplot(mtcars, aes(wt, mpg)) + geom_point()
  }, res = 96)
  
  my_data <- eventReactive(input$plot_brush, {
    if (rv$table == 1) {
      rv$table <- 2
      rv$data1 <- input$plot_brush
    } else {
      rv$table <- 1
      rv$data2 <- input$plot_brush
    }
    return(rv)
  })
  
  output$data1 <- renderTable({
    brushedPoints(mtcars, my_data()$data1)
  })
  
  output$data2 <- renderTable({
    brushedPoints(mtcars, my_data()$data2)
  })
  
}

shinyApp(ui=ui, server=server)