Home > Blockchain >  How to create working dynamic tab boxes in shiny app
How to create working dynamic tab boxes in shiny app

Time:09-22

I am trying to create dynamic tabBoxes with a selectize input in a shiny app
I get the tabs to render with the correct label
However, the gt table does not seem to update correctly
It only produces the same gt table across all tabs

My data actual data is a list with multiple dataframes

Any ideas where I am going wrong?

I am trying to following along this 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() 
                                                        
                                                        
                                                        
                                                        
                                                        
                                              })
                                              
                                              
                                              
                                    })
                          }
                          
                          
                })
                
                
      }
)

CodePudding user response:

Try this

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

it works fine for me.

  • Related