【问题标题】:How to keep track of nested tables in reactable?如何跟踪可反应的嵌套表?
【发布时间】:2021-11-16 02:16:35
【问题描述】:

我有一个问题,我不太确定如何解决。考虑这个闪亮的应用示例:

library(shiny)
library(reactable)
library(dplyr)


random_requests <- data.frame(
    Request_ID = c(1,2,3,4,5),
    Status = c("Accepted", "Accepted", "Accepted", "Declined", "Created")
)

random_samples <- data.frame(
    Request_ID = c(1,1,1,2,3,3,4,5),
    Sample_ID = c(1,2,3,4,5,6,7,8),
    statistics = sample(1:100, 8)
)


# Define UI for application 
ui <- fluidPage(
    
   selectInput(inputId = "select",
               label = "Select a choice:",
               choices = c("All", "Accepted", "Declined", "Created")),
    hr(),
    reactableOutput(outputId = "table")
)

# Define server logic 
server <- function(input, output) {
    
    
    data <- eventReactive(input$select, {
        if(input$select == "All"){
            random_requests
        } else {
            random_requests %>%
                filter(Status == input$select)
        }
    })
    
    
    output$table <- renderReactable({
        
        reactable(
            data(),
            details = function(index, name){
                htmltools::div(
                    reactable(random_samples[random_samples$Request_ID == index, ])
                )
            }
            )
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

我想使用expandable rows feature of reactable。主表应显示请求,然后当您展开该行时,子表应显示与该特定请求关联的样本。当表是静态的时,这很好用,但是当我使用下拉列表进行过滤时,它的行为并不符合预期。当前子表按行索引匹配,所以当我过滤表时,示例子表与正确的请求不匹配。

我怎样才能实现这个功能?即如何在details函数内部通过Request_ID链接random_requests和random_samples表,使其在过滤时起作用?

谢谢! -凯尔

【问题讨论】:

    标签: r shiny nested-table reactable


    【解决方案1】:

    包开发者 Greg Lin 回答:https://github.com/glin/reactable/issues/199#issuecomment-933003834

    这个功能可以通过修改reactable代码来实现:

    output$table <- renderReactable({
        
        reactable(
          data(),
          details = function(index, name){
            request_id <- data()[index, "Request_ID"]
            htmltools::div(
              reactable(random_samples[random_samples$Request_ID == request_id, ])
            )
          }
        )
      })
    

    【讨论】:

      猜你喜欢
      • 2012-06-09
      • 2019-09-08
      • 2019-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-12
      • 1970-01-01
      • 2019-12-11
      相关资源
      最近更新 更多