首次加载时填充selectizeInput

时间:2018-03-22 14:36:58

标签: r shiny shinydashboard

下面是一个非常简单的闪亮仪表板的代码。在选项卡1“选择”我有一个单选按钮选择器,在选项卡2“食物”我有一个selectizeInput。

当仪表板最初加载时,第一个选项卡已加载,默认情况下选择“Fruits”。然而,当我移动到标签2时,selectizeInput下拉菜单中没有任何内容显示,其中一个观察事件链接到单选按钮。

如果我然后返回“选择”选项卡并单击“Meats”,则会选择selectizeInput。如果我在Select选项卡上再次选择Fruits,则selectizeInput会填充一个水果列表。

如何使用水果列表首次加载selectizeInput?

由于

library(shinydashboard)
library(data.table)

menu <- data.table(numb = c(rep(1,4), rep(2,4)),
               item = c("Apple", "Orange", "Grape", "Lemon", "Steak", "Chicken", "Pork", "Venison"))


ui <- dashboardPage(skin = "blue",
                    dashboardHeader(),
                    dashboardSidebar(
                      sidebarMenu(id = "initalTab",
                                  sidebarMenuOutput("menuSidebar"))
                      ),
                    dashboardBody(
                        tabItems(
                          tabItem("select",
                                  uiOutput("selectType")),
                          tabItem("food",
                                  uiOutput("selectFood"))
                          )
                    )
)

server <- (function(input, output, session) {

  output$menuSidebar <- renderMenu({
    sidebarMenu(
      menuItem("Select", tabName = "select", icon = icon("home")),
      menuItem("Food", tabName = "food", icon = icon("sort"))
    )
  })

  isolate({updateTabItems(session, "initalTab", "select")})

  output$selectType <- renderUI({
    fluidRow(
      box(width = 3, status = "primary", solidHeader = TRUE,
          radioButtons("foodFilter", label = h4("Filter by Food Type"),
                       choices = c("Fruits" = 1, "Meats" = 2),
                       selected = 1,
                       inline = TRUE)
      )
    )
  })

  output$selectFood <- renderUI({
    fluidRow(
      box(width = 6, status = "primary", solidHeader = TRUE,
      h4("Select Your Food"),
      selectizeInput("group",
                     choices = NULL,
                     width ="100%",
                     NULL,
                     NULL,
                     multiple = TRUE,
                     options = list(plugins = list("drag_drop", "remove_button"),
                                    placeholder = "Please select you food"))
      )
    )
  })

  observeEvent(input$foodFilter, {
    updateSelectizeInput(session,
                     "group",
                     choices = menu[numb == input$foodFilter,`item`],
                     selected = menu[numb == input$foodFilter,`item`][1],
                     server = TRUE)
  })
})


shinyApp(ui, server)

1 个答案:

答案 0 :(得分:1)

在我的评论的基础上尝试添加此行outputOptions(output, "selectFood", suspendWhenHidden = FALSE)

library(shinydashboard)
library(data.table)
library(shiny)

menu <- data.table(numb = c(rep(1,4), rep(2,4)),
                   item = c("Apple", "Orange", "Grape", "Lemon", "Steak", "Chicken", "Pork", "Venison"))


ui <- dashboardPage(skin = "blue",
                    dashboardHeader(),
                    dashboardSidebar(
                      sidebarMenu(id = "initalTab",
                                  sidebarMenuOutput("menuSidebar"))
                    ),
                    dashboardBody(
                      tabItems(
                        tabItem("select",
                                uiOutput("selectType")),
                        tabItem("food",
                                uiOutput("selectFood"))
                      )
                    )
)

server <- (function(input, output, session) {

  output$menuSidebar <- renderMenu({
    sidebarMenu(
      menuItem("Select", tabName = "select", icon = icon("home")),
      menuItem("Food", tabName = "food", icon = icon("sort"))
    )
  })

  isolate({updateTabItems(session, "initalTab", "select")})

  output$selectType <- renderUI({
    fluidRow(
      box(width = 3, status = "primary", solidHeader = TRUE,
          radioButtons("foodFilter", label = h4("Filter by Food Type"),
                       choices = c("Fruits" = 1, "Meats" = 2),
                       selected = 1,
                       inline = TRUE)
      )
    )
  })

  output$selectFood <- renderUI({
    fluidRow(
      box(width = 6, status = "primary", solidHeader = TRUE,
          h4("Select Your Food"),
          selectizeInput("group",
                         choices = NULL,
                         width ="100%",
                         NULL,
                         NULL,
                         multiple = TRUE,
                         options = list(plugins = list("drag_drop", "remove_button"),
                                        placeholder = "Please select you food"))
      )
    )
  })

  outputOptions(output, "selectFood", suspendWhenHidden = FALSE)  

  observeEvent(input$foodFilter,{
    updateSelectizeInput(session,
                         "group",
                         choices = menu[numb == input$foodFilter,`item`],
                         selected = menu[numb == input$foodFilter,`item`][1],
                         server = TRUE)
  })
})


shinyApp(ui, server)