【问题标题】:selectInput with RenderUI doesn't show all choices带有 RenderUI 的 selectInput 不显示所有选择
【发布时间】:2020-07-02 23:41:02
【问题描述】:

我正在开发一个闪亮的应用程序,其中包含使用 renderUI 创建的动态 selectInput。
这是server.R中的代码:

  output$list_station<- renderUI({
   
    choix_ce <- input$choice_of_ce
    
    postes <- map_consos_enedis %>% 
      filter(Region == choix_ce) %>% 
      select(station)
    
    selectInput(inputId = "choice_of_station",
                label = "Choix du poste",
                choices = as.list(postes),
                selected = postes[1],
                multiple = FALSE)
  })

它几乎可以正常工作...仅当 postes 返回 2 个或更多元素时。如果 postes 返回 1 个元素,下拉列表只包含 station 这是 postes 的变量名,我不明白为什么。

【问题讨论】:

  • 你能提供一个 dput(postes) 的例子来帮助理解发生了什么吗?
  • 当然!这里 dput(postes) 如果 postes 包含 1 个元素:structure(list(station = "AUCUN"), row.names = c(NA, -1L), class= c("tbl_df", " tbl”,“data.frame”))。并带有 2 个或更多元素(所有元素均未显示):结构(列表(站 = c(“A.DUCP3”,“A.OTHP3”,“ABBENP3”,“ABBENP3”,[........ ],“VOUJEP3”,“VOUJEP3”,“WADONP3”,“WALDIP3”,“WALDIP3”)),row.names = c(NA,-603L),class= c(“tbl_df”,“tbl”,“数据.frame"))

标签: r shiny


【解决方案1】:

您应该使用postes$station 作为choices 参数,而不是as.list(postes)

selectInput(inputId = "choice_of_station",
            label = "Choix du poste",
            choices = postes$station,
            selected = postes$station[1],
            multiple = FALSE)

choicesselectInput 的参数可以作为参数的原因:

  1. list
  2. 一个名为list
  3. 一个名为list 的群组,请参阅doc

通过使用postes$stationpostes 数据帧被简化为一个没有名称的简单字符向量,即选项 1),它给出了您期望的输出。

当您在 postes 中只有 1 行时,as.list(postes) 成为命名列表 list(station='AUCUN'),这使得 selectInput 使用选项 2,即显示名称 station 但选择值 'AUCUN' 并且是你的问题。

postes 中有很多行时,as.list(postes) 成为命名列表 与使 selectInput 使用选项 3 的组。

【讨论】:

  • 我正在修改原因:我找到了真正的;) - 给我 3 分钟
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-05
  • 2021-07-07
  • 1970-01-01
  • 2015-10-27
  • 1970-01-01
相关资源
最近更新 更多