根据另一个数据表的某些单元格选择以及在闪亮应用程序的活动选项卡中显示数据表 [英] Display datatable based on certain cell selection of another datatable and in active tab in a shiny app

查看:80
本文介绍了根据另一个数据表的某些单元格选择以及在闪亮应用程序的活动选项卡中显示数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面有一个闪亮的仪表板,在其中的 Documents 标签中,显示 iris 数据集的前两行。

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

单击列 Species 的任何单元格时,我会自动移至视图标签。

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

但是我需要以下功能。

当用户单击文档 选项卡中第一行的setosa 单元格视图中侧边栏中的数据表标签仅应显示 iris 数据集。当我单击 Documents 选项卡第二行的 setosa 单元格时,在查看 选项卡仅应显示另一个数据框,例如 mtcars

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


推荐答案

像这样的事情?

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

这篇关于根据另一个数据表的某些单元格选择以及在闪亮应用程序的活动选项卡中显示数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆