简体   繁体   English

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

[英]How to dynamically pick dataset in selectizeInput depending on tab selection in shinydashboard

I have the following app created in shiny using shinydashboard我使用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)

I'm trying to dynamically select the inputs in my selectizeInput (Line 21) depending on which tabPanel selected.我试图根据选择的tabPanel在我的selectizeInput (第 21 行)中动态选择输入。 For example, if I have the Production tab selected (line 28), I want to pass the production_data dataframe as the options in selectizeInput placeholder.例如,如果我选择了Production选项卡(第 28 行),我想将production_data数据框作为selectizeInput占位符中的选项传递。 Similarly, I want the housing_data dataframe to be selected if I'm on the housing tab (line 29).同样,如果我在housing选项卡上(第 29 行),我希望选择housing_data数据框。

Is there a way to dynamically select the dataframe in line 22 (it's currently just production_data ) depending on which tab I'm on in the app?有没有办法根据我在应用程序中所在的选项卡动态选择第 22 行中的数据框(目前只是production_data )?

Using an updateSelectizeInput inside an observe r you could do: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)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM