首页 > 解决方案 > 单击 DTdatatable 中的单元格并移动到闪亮应用程序的另一个选项卡

问题描述

我在下面有闪亮的仪表板,我想知道是否可以使列的单元格Species交互,例如,如果用户单击该列的某个单词'setosa',以移动到选项卡Species

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(DT)
shinyApp(
  ui = dashboardPagePlus(
    header = dashboardHeaderPlus(
    ),
    sidebar = dashboardSidebar(),
    body = dashboardBody(
      tabsetPanel(
        tabPanel("Documents",
                 DTOutput("dt1")),
        tabPanel("Species")
      )
    ),

  ),
  server = function(input, output) {
    library(readxl)

    output$dt1<-renderDT(
      iris,filter = "top",
      options = list(
        pageLength = 5
      )
    )

  }
)

标签: rshinydt

解决方案


请检查以下内容:

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

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

  ),
  server = function(input, output, session) {
    output$dt1 <- renderDT(
      iris,
      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")
      }
    })

  }
)

推荐阅读