【问题标题】:How do I display detailed table on the basis of inputs from three different summary tables using DT / renderDataTable() in r shiny如何在 r shiny 中使用 DT / renderDataTable() 根据三个不同汇总表的输入显示详细表
【发布时间】:2021-03-20 17:36:45
【问题描述】:

我有三个不同的数据框,它们显示了具有不同行数和列数的完全不同的数据。我在闪亮屏幕的左栏中将它们全部显示在另一个下方。在右侧,我想显示一个详细的数据框,具体取决于左侧选定表的选定行(三行中)。

input$tableId_cell_clickedinput$tableId_rows_selected 非常有用,如果我必须从一个数据帧中选择并根据所选行或单击单元格显示其他数据帧。

是否有任何输入参数可以帮助我获取用户单击的表格/数据框(三个)以及所选的单元格或行。这将使我能够根据用户输入的表格和行在闪亮屏幕的右侧显示详细表格?

示例如下:

library(shiny)
library(DT)                     # datatable()
library(shinydashboard)


ui <-fluidPage(
  dashboardPage(
    dashboardHeader(disable = TRUE),
    dashboardSidebar(disable = TRUE, width = NULL, collapsed = TRUE),
    dashboardBody(            
      fixedRow(
        column(4, 
               dataTableOutput("summary_abc"), 
               dataTableOutput("summary_def"), 
               dataTableOutput("summary_ghi")),
        column(5, 
               textOutput("employee_details")))
    )))

server <- shinyServer(function(input, output, session) {
  output$summary_abc <- renderDataTable({
    options(DT.options = list(pageLength = 10, searching = FALSE, paging = FALSE))
    
    employee <- c('John Doe','Peter Gynn','Jolie Hope')
    salary <- c(21000, 23400, 26800)
    age <- c(45,63,28)
    data1 <- data.frame(employee, salary, age)
    
    table_1 <- data1
    table_1 <- datatable(table_1, class = 'cell-border stripe', selection = "single",
                         options = list(ordering=F, dom = 't'),
                         caption = "Summary 1", rownames = FALSE)
    table_1})
  
  output$summary_def <- renderDataTable({
    options(DT.options = list(pageLength = 10, searching = FALSE, paging = FALSE))
    
    employee <- c('John Doe','Peter Gynn','Jolie Hope')
    qualification <- c("Graduate", "Post Graduate", "Master")
    experience <- c(8,5,17)
    data2 <- data.frame(employee, qualification, experience)
    
    table_1 <- data2
    table_1 <- datatable(table_1, class = 'cell-border stripe', selection = "single",
                         options = list(ordering=F, dom = 't'),
                         caption = "Summary 2", rownames = FALSE)
    table_1})
  
  output$summary_ghi <- renderDataTable({
    options(DT.options = list(pageLength = 10, searching = FALSE, paging = FALSE))
    
    employee <- c('John Doe','Peter Gynn','Jolie Hope', "Jackson")
    weight <- c(60,58,72,59)
    temperature <- c(95,97,96,98)
    data3 <- data.frame(employee, weight, temperature)
    
    table_1 <- data3
    table_1 <- datatable(table_1, class = 'cell-border stripe', selection = "single",
                         options = list(ordering=F, dom = 't'),
                         caption = "Summary 3", rownames = FALSE)
    table_1})
  
  
  output$employee_details <- renderText(
    {
      table_selected <- "Summary 2"
      row_selected <- 3
      paste("Table: ", table_selected, "Row: ", row_selected)
      
    }
  )
  
  
  
})

shinyApp(ui = ui, server = server)

【问题讨论】:

  • 您可以根据用户单击的行过滤 3 个数据框,并将所有 3 个选定的行合并到一个新的数据框中,您可以在右侧显示该数据框。为了真正帮助您,您能否发布一个最小的工作示例?
  • 您好托马斯,感谢您的回复。请参阅附加示例。我想提取用户选择的表名和行号。
  • 感谢您的示例!下一次,尽量减少代码,这样更容易直接关注你的问题。这里所有的 CSS 和 JS 代码以及 reactable 包都与解决您的问题完全无关
  • 效果很好,正是我想要的。谢谢。下次我会记住发布有限的代码。

标签: r shiny datatables interactive


【解决方案1】:

您可以使用响应式值来保存最新的表格点击和每次更改的任意更多信息。

将此添加到您的服务器功能并更改您的文本输出:

table_reac <- reactiveValues()
observeEvent(input$summary_abc_rows_selected, {
  table_reac$title <- "Summary 1"
  table_reac$row <- input$summary_abc_rows_selected
})
observeEvent(input$summary_def_rows_selected, {
  table_reac$title <- "Summary 2"
  table_reac$row <- input$summary_def_rows_selected
})
observeEvent(input$summary_ghi_rows_selected, {
  table_reac$title <- "Summary 3"
  table_reac$row <- input$summary_ghi_rows_selected
})

output$employee_details <- renderText(
  {
    table_selected <- table_reac$title
    row_selected <- table_reac$row
    
    paste("Table: ", table_selected, "Row: ", row_selected)
    
  }
)

【讨论】:

    猜你喜欢
    • 2021-09-30
    • 2018-10-08
    • 1970-01-01
    • 2018-05-06
    • 2018-10-07
    • 2018-07-08
    • 1970-01-01
    • 2015-10-07
    • 2020-05-02
    相关资源
    最近更新 更多