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

血红的双手。 提交于 2020-06-17 09:51:06

问题


I have the shiny dashboard below in which in tab Documents I display the first 2 lines of the iris dataset.

When I click on any cell of the column Species I automatically move to the View tab.

But I need the functionality described below.

When the user clicks on the setosa cell of the 1st row in Documents tab the datatable in the sidebar in the View tab only should display iris dataset. When I click on the setosa cell of the the 2nd row in Documents tab the datatable in the sidebar in the View tab only should display another dataframe, lets say 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)
      }
    )
  }
)

回答1:


Something like this?

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
      }
    )
  }
)


来源:https://stackoverflow.com/questions/61911536/display-datatable-based-on-certain-cell-selection-of-another-datatable-and-in-ac

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!