在renderUI中使用时,shinyWidgets :: pickerInput不出现

时间:2017-10-30 04:42:09

标签: r shiny

我不明白为什么以下代码无效:

library(shinyWidgets)
library(shiny)
library(tidyverse)
library(forcats)

tmp <- tibble(Cat = c(rep("IV", 5), rep("III", 5)),
       Subcat = LETTERS[1:10])
tmp <- mutate(tmp, Cat = factor(Cat), Subcat = factor(Subcat))       

ui <- fluidPage(
       titlePanel(""),
   sidebarLayout(
      sidebarPanel(
         checkboxGroupButtons("Cat", "Category:", choices = levels(tmp$Cat), selected = levels(tmp$Cat)),
         uiOutput("Subcat")
      ),
   mainPanel(tableOutput("Table"))
   )
)

server <- function(input, output) {
   output$Subcat <- renderUI({
     tmp <- tmp %>% 
       filter(Cat %in% input$Cat) %>% 
       mutate(Subcat = fct_drop(Subcat))

     # pickerInput("what", label = "hello?", choices = levels(tmp$Subcat), selected = levels(tmp$Subcat), multiple = TRUE) #Does not appear
     checkboxGroupButtons("what", label = "hello?", choices = levels(tmp$Subcat), selected = levels(tmp$Subcat)) #Appears
   })

   output$Table <- renderTable({ filter(tmp, Cat %in% input$Cat, Subcat %in% input$what)})
   }

shinyApp(ui = ui, server = server)

如果我取消注释pickerInput行并注释掉以下行,则不会显示下拉窗口小部件。我做错了什么,或者我偶然发现了一个错误?

编辑:我做错了。我需要使用updatePickerInput()

1 个答案:

答案 0 :(得分:2)

我在版本0.3.5上,一切都适合我。尝试安装开发版

# install.packages("devtools")
# devtools::install_github("dreamRs/shinyWidgets")


library(shinyWidgets)
library(shiny)
library(tidyverse)
library(forcats)

tmp <- tibble(Cat = c(rep("IV", 5), rep("III", 5)),
              Subcat = LETTERS[1:10])
tmp <- mutate(tmp, Cat = factor(Cat), Subcat = factor(Subcat))       

ui <- fluidPage(
  titlePanel(""),
  sidebarLayout(
    sidebarPanel(
      checkboxGroupButtons("Cat", "Category:", choices = levels(tmp$Cat), selected = levels(tmp$Cat)),
      uiOutput("Subcat")
    ),
    mainPanel(tableOutput("Table"))
  )
)

server <- function(input, output) {
  output$Subcat <- renderUI({
    tmp <- tmp %>% 
      filter(Cat %in% input$Cat) %>% 
      mutate(Subcat = fct_drop(Subcat))
    pickerInput("what", label = "hello?", choices = levels(tmp$Subcat), selected = levels(tmp$Subcat), multiple = TRUE) #Does not appear
  })

  output$Table <- renderTable({ filter(tmp, Cat %in% input$Cat, Subcat %in% input$what)})
}

shinyApp(ui = ui, server = server)

enter image description here