如何根据shinydashboard中的选项卡选择在selectizeInput中动态挑选数据集

0g0grzrc  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(107)

我使用shinydashboard在shiny中创建了以下应用程序

library(shiny)
library(shinydashboard)

production_data <- data.frame(Variable = c('GDP', 'IP', 'Manufacturing'),
                         Value = c(1,2,3)) %>% 
                   as_tibble()

housing_data <- data.frame(Variable = c('Prices', 'Sales', 'Mortgages'),
                           Value = c(1,2,3)) %>% 
                as_tibble()

ui <- dashboardPage(
                    dashboardHeader(title = 'Dashboard'),
                    dashboardSidebar(sidebarMenu(
                                                 menuItem(tabName = 'Panel1', text = 'Heatmaps'),
                                                 menuItem(tabName = 'Panel2', text = 'Linecharts')
                                                )
                                     ),
                    dashboardBody(tabItems(tabItem(tabName = 'Panel1',
                                                     fluidRow(box(selectizeInput('select', 'Select Variable', 
                                                                                 choices = production_data %>% select(Variable) 
                                                                                 ), height=80,width=4,
                                                                  )
                                                              ),
                                                     fluidRow(tabBox(
                                                                     id = 'tabset1', width = 13, height = 655,
                                                                     tabPanel('Production', height = 655),
                                                                     tabPanel('Housing', height = 655)
                                                                     )
                                                              )
                                                   )
                                           )
                                  )
                   )

server <- function(input, output) {
  
  
}

shinyApp(ui, server)

我尝试动态选择selectizeInput中的输入(第21行),具体取决于所选的tabPanel。例如,如果我选择了Production选项卡(第28行),我想传递production_data Dataframe 作为selectizeInput占位符中的选项。如果我在housing选项卡上(第29行),我希望选择housing_data Dataframe 。
有没有办法根据我在应用程序中的哪个选项卡动态选择第22行中的 Dataframe (目前只有production_data)?

z2acfund

z2acfund1#

observe r中使用updateSelectizeInput可以执行以下操作:

library(shiny)
library(shinydashboard)
library(tibble)

production_data <- data.frame(
  Variable = c("GDP", "IP", "Manufacturing"),
  Value = c(1, 2, 3)
) %>%
  as_tibble()

housing_data <- data.frame(
  Variable = c("Prices", "Sales", "Mortgages"),
  Value = c(1, 2, 3)
) %>%
  as_tibble()

ui <- dashboardPage(
  dashboardHeader(title = "Dashboard"),
  dashboardSidebar(sidebarMenu(
    menuItem(tabName = "Panel1", text = "Heatmaps"),
    menuItem(tabName = "Panel2", text = "Linecharts")
  )),
  dashboardBody(tabItems(tabItem(
    tabName = "Panel1",
    fluidRow(box(selectizeInput("select", "Select Variable",
      choices = production_data %>% select(Variable)
    ), height = 80, width = 4, )),
    fluidRow(tabBox(
      id = "tabset1", width = 13, height = 655,
      tabPanel("Production", height = 655),
      tabPanel("Housing", height = 655)
    ))
  )))
)

server <- function(input, output) {
  observe({
    choices <- if (input$tabset1 == "Production") {
      unique(production_data$Variable)
    } else {
      unique(housing_data$Variable)
    }
    updateSelectizeInput(inputId = "select", choices = choices)
  })
}

shinyApp(ui, server)

相关问题