根据另一个数据表的某些单元格选择并在闪亮应用程序的活动选项卡中显示数据表

Display datatable based on certain cell selection of another datatable and in active tab in a shiny app

我有下面闪亮的仪表板,在选项卡 Documents 中,我显示了 iris 数据集的前两行。

当我点击 Species 列的任何单元格时,我会自动移动到 View 选项卡。

但我需要下面描述的功能。

当用户单击 Documents 选项卡第一行的 setosa 单元格时,View 选项卡侧边栏中的数据表应仅显示 iris 数据集.当我单击 Documents 选项卡中第二行的 setosa 单元格时,View 选项卡侧边栏中的数据表仅应显示另一个数据框,假设为 mtcars

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(DT)

shinyApp(
  ui = dashboardPagePlus(
    header = dashboardHeaderPlus(),
    sidebar = dashboardSidebar(
      DT::DTOutput("dt2")
    ),
    body = dashboardBody(tabsetPanel(
      id = "myTabsetPanel",
      tabPanel("Documents",
               DTOutput("dt1")),
      tabPanel("Species")
    )),

  ),
  server = function(input, output, session) {
    output$dt1 <- renderDT(
      iris[1:2,],
      filter = "top",
      options = list(pageLength = 5),
      selection = list(mode = 'single', target = 'cell')
    )

    observeEvent(input$dt1_cell_clicked, {
      # alternative: input$dt1_cells_selected
      if (req(input$dt1_cell_clicked$value) == "setosa") {
        updateTabsetPanel(session, inputId = "myTabsetPanel", selected = "Species")
      }
    })
    output$dt2<-renderDT(
      if(input$myTabsetPanel=="Species"){
        iris
      }
      else{
        return(NULL)
      }
    )
  }
)

是这样的吗?

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(DT)
library(datasets)

shinyApp(
  ui = dashboardPagePlus(
    header = dashboardHeaderPlus(),
    sidebar = dashboardSidebar(),
    body = dashboardBody(tabsetPanel(
      id = "myTabsetPanel",
      tabPanel("Documents",
               DT::DTOutput("dt1")),
      tabPanel("Species",
               DT::DTOutput("dt2"))
    )),

  ),
  server = function(input, output, session) {
    output$dt1 <- renderDT(
      iris[1:2,],
      filter = "top",
      options = list(pageLength = 5),
      selection = list(mode = 'single', target = 'cell')
    )

    observeEvent(input$dt1_cell_clicked, {
      # alternative: input$dt1_cells_selected
      if (req(input$dt1_cell_clicked$value) == "setosa") {
        updateTabsetPanel(session, inputId = "myTabsetPanel", selected = "Species")
      }
    })

    output$dt2 <- renderDT(
      if(input$dt1_cell_clicked$row == 1){
        iris
      }
      else{
        mtcars
      }
    )
  }
)