0

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

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)

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

我怎样才能实现这个功能?Request_ID也就是说,如何在函数内部链接 random_requests 和 random_samples 表,details以便在过滤时起作用?

谢谢!-凯尔

4

1 回答 1

0

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

可以通过将可反应代码修改为:

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, ])
        )
      }
    )
  })
于 2021-10-04T14:04:14.187 回答