簡體   English   中英

在Shiny中,可以將數據幀用作selectizeInput中的選擇嗎?

[英]In Shiny can a data frame be used as choices in selectizeInput?

selectizeInput中的選擇是否可能是數據幀中的行? 如果是這樣,返回的數據將是所選行的項目列表嗎? 我一直無法完成這項工作。 在下面的代碼中,cityInput有效,因為選擇是一個字符向量; 但是locationInput不起作用,選擇框中的項目列表為空。

這是常見的情況,其中用戶輸入需要根據多個列中的值進行選擇以確定唯一的行。 在下面的示例中,不同的城市具有相同的名稱,並且使用州來唯一地確定位置。 將兩列粘貼在一起是一種解決方案,但是在復雜情況下,這種方法會變得混亂。

library(shiny)

locations <- data.frame(City=c("Ames", "Beaumont", "Beaumont", "Portland", "Portland"),
                        State=c("IA", "CA", "TX", "ME", "OR"))

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectizeInput("cityInput", "City", choices=NULL, selected=NULL),
      selectizeInput("locationInput", "Location", choices=NULL, selected=NULL)
    ),
    mainPanel("Main Panel")
  )
)

server <- function(input, output, session) {
  updateSelectizeInput(session, 'cityInput',
              choices = locations$City,
              server = TRUE
  )
  updateSelectizeInput(session, 'locationInput',
              choices = locations,
              server = TRUE
  )
}

shinyApp(ui, server)

顯然selectizeInput需要將selectizeInput的列稱為valuelabel

然后顯示一些位置信息:

library(shiny)

locations <- data.frame(value=c("Ames", "Beaumont", "Beaumont", "Portland", "Portland"),
                        label=c("IA", "CA", "TX", "ME", "OR"))


ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectizeInput("cityInput", "City", choices=NULL, selected=NULL),
      selectizeInput("locationInput", "Location", choices=NULL, selected=NULL)
    ),
    mainPanel("Main Panel")
  )
)

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

  updateSelectizeInput(session, 'cityInput',
                       choices = locations$value,
                       server = TRUE
  )
  updateSelectizeInput(session, 'locationInput',
                       choices = locations,
                       server = TRUE
  )
}

shinyApp(ui, server)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM