如何在闪亮的应用程序中创建工作动态选项卡框

How to create working dynamic tab boxes in shiny app

我正在尝试在闪亮的应用程序中创建带有选择输入的动态选项卡框
我使用正确的标签
来渲染标签 但是,gt table 似乎没有正确更新
它只会在所有选项卡

中产生相同的 gt table

我的数据实际数据是一个包含多个数据框的列表

我哪里出错了?

我正在努力跟进example

library(shiny)
library(tidyverse)
library(gt)
library(shinydashboard)



d <- list(
      
      data.frame(
                state = c("ny", "ny", "ny"),
                name = c("joe", "john", "jim"), 
                pet = c("cat", "cat", "cat, dog"), 
                age = c(34, 35, 36)
      ),
      
      data.frame(
                state = c("ak", "ak", "ak"),
                name = c("jane", "dan", "don"), 
                pet = c("snake, cat", "snake, cat", "snake"),
                age = c(34, 35, 36)
      ),
      
      data.frame(
                state = c("fl", "fl", "fl"),
                name = c("mat", "lucy", "noel"), 
                pet = c("fish", "fish, dog", "fish"),
                age = c(34, 35, 36)
      )
)


raw <- rbind(d[[1]], d[[2]], d[[3]])


shinyApp(
      
      
      ui = fluidPage(
                
                selectizeInput("pick_keys", 
                               "Pick keywords", 
                               choices = NULL, 
                               selected = NULL, 
                               multiple = TRUE,
                               options = NULL), 
                
                
                # textOutput("text"), 
                
                # dynamic UI for gt tables
                
                uiOutput("tables")
                
                
      ), 
      
      server = function(input, output, session){
                
                
                
                
                updateSelectizeInput(session, "pick_keys", choices = raw$pet, server = TRUE)
                
                
                
                
                search_keys <- reactive({
                          
                          paste(input$pick_keys, collapse = "|")
                          
                })
                
                mylist <- eventReactive(input$pick_keys, {
                          
                          
                          df <- raw %>%
                                    filter(str_detect(pet, as.character(search_keys()))) %>%
                                    group_split(state) 
                          
                          
                          
                          return(df)
                          
                          
                })
                
                
                observeEvent(mylist(), {
                          
                          
                          # Insert the right number of table output objects into the web page
                          
                          
                          output$tables <-  renderUI({
                                    
                                    # list of output objects
                                    
                                    table_output_list <- list()
                                    
                                    for(i in 1:length(mylist())){
                                              
                                              tablename <- paste("table", i, sep="")
                                              
                                              
                                              table_output_list[i] <- tagList(
                                                        tabPanel(
                                                                  title = unique(mylist()[[i]]$state), 
                                                                  gt_output(tablename)
                                                        )
                                              )
                                              
                                              
                                    }
                                    
                                    do.call(tabBox, table_output_list)
                          })
                          
                          
                          
                          
                          
                          
                          for (i in 1:length(mylist())){
                                    
                                    local({
                                              my_i <- i
                                              
                                              tablename <- paste(
                                                        "table",
                                                        my_i,
                                                        sep = "")
                                              
                                              output[[tablename]] <- render_gt({
                                                        
                                                        mylist()[[i]] %>%
                                                                  
                                                                  gt() 
                                                        
                                                        
                                                        
                                                        
                                                        
                                              })
                                              
                                              
                                              
                                    })
                          }
                          
                          
                })
                
                
      }
)

试试这个

   local({
          my_i <- i
          
          #tablename <- paste0("table", my_i)
          
          output[[paste0("table", my_i)]] <- render_gt({
            mylist()[[my_i]] %>%
              gt() 
          })
        })

对我来说效果很好。