可写的selectInput选择器

时间:2017-11-09 09:12:19

标签: css r shiny

我想以一种'可写'的方式修改我的selectInput。例如,'location'包含Amsterdam,Rotterdam,Den Haag等。我想实现一个'可写'的inputselector。因此,当有人写“Ams ..”或“Amster ...”时,会显示符合此标准的可能位置。 有人能帮帮我吗?提前谢谢。

这是我简化的 ui 部分:

library(DT)
library(shiny)
library(shinydashboard)

Location = c("Amsterdam","Amsterdam","Amsterdam","Rotterdam","Rotterdam","Rotterdam","Den Haag","Den Haag","Den Haag") 
Year = c(2015,2014,2016,2015,2016,2016,2017,2016,2014) 
Person = c("John", "Ann", "Katy", "Ann", "Katy", "William", "Henry", "Luke", "Luke")
mockup = data.frame(Location, Year, Person)

ui <- dashboardPage(
                    dashboardHeader(),
                    dashboardSidebar(
                      sidebarMenu(
                        menuItem("Selection", tabName = "selection")                  
                                  )
                    ),
                    dashboardBody(
                      tabItems(
                        tabItem(tabName = "selection",
                                fluidRow(
                                  box(width = 5, 
                                      title = "TITLE", "Choose something",
                                      collapsible = FALSE, 
                                      htmlOutput("Location_selector"),
                                      htmlOutput("Year_selector")
                                  ),
                                  mainPanel(
                                    DT::dataTableOutput("selection")
                                  )
                                 ))             

                                        )
                      )
                      )

这是我的服务器部分:

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

  output$Location_selector = renderUI({
    selectInput(inputId = "Location",
                label = "Location:",
                choices = sort(as.character(unique(mockup$Location))))
  })
  output$Year_selector = renderUI({

    data_available = mockup[mockup$Location == input$Location, "Year"]

    selectInput(inputId = "Year",
                label = "Year:",
                choices = sort(unique(data_available)),
                selected = unique(data_available)[1])
  })

}

shinyApp(ui, server)

1 个答案:

答案 0 :(得分:0)

我在SO上找到了我自己的问题的答案。我通过添加一个空选项(“”)来调整服务器部分的“选择”部分:

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

  output$Location_selector = renderUI({
    selectInput(inputId = "Location",
                label = "Location:",
                choices = c("", (sort(as.character(unique(mockup$Location))))))
  })
}

shinyApp(ui, server)