闪亮 - 更改 selectInput() 中的选项数量
Shiny - Changing the number of choices in selectInput()
我想更改 selectInput() 中的选项数。如果新选择的数量与原始选择的数量相等,则以下 reprex 有效,但如果提供了更多(或更少)选择,则代码不起作用。我怎样才能变得闪亮,不仅接受 selectInput() 的新选择,而且接受新的选择数量?在此先感谢您的帮助。
菲利普
library(shiny)
ui <- fluidPage(
tabPanel("tbls",
selectInput("tab1",label="Pick a table:",choices=c("a","b","c")),
selectInput("cht1",label="Pick a time series:",choices=c("d","e","f"))
)
)
server <- function(input,output,session) {
Nchoices <- reactive({case_when(
input$tab1=="a" ~c("d","e","f"),
input$tab1=="b" ~c("g","h","i"),
input$tab1=="c" ~c("j","k","l","m") # adding one more choice breaks the code
)})
observe({updateSelectInput(session,"cht1",
label="Pick a time series:",choices=Nchoices(),selected=NULL)})
observe(print(Nchoices()))
}
shinyApp(ui, server)
尝试使用 switch
而不是 case_when
。此外,renderUI
可能会有用。试试这个
library(shiny)
ui <- fluidPage(
tabPanel("tbls",
selectInput("tab1",label="Pick a table:",choices=c("a","b","c")),
uiOutput("myselect")
#selectInput("cht1",label="Pick a time series:",choices=c("d","e","f"))
)
)
server <- function(input,output,session) {
Nchoices <- reactive({
switch(input$tab1,
"a" = c("d","e","f"),
"b" = c("g","h"),
"c" = c("j","k","l","m") # adding one more choice breaks the code
)
})
output$myselect <- renderUI({
req(input$tab1)
selectInput("cht1",label="Pick a time series:",choices=Nchoices())
})
observe(print(Nchoices()))
}
shinyApp(ui, server)
请注意,在 case_when
中,所有 RHS 值都必须是同一类型。不一致的类型将引发错误。
我想更改 selectInput() 中的选项数。如果新选择的数量与原始选择的数量相等,则以下 reprex 有效,但如果提供了更多(或更少)选择,则代码不起作用。我怎样才能变得闪亮,不仅接受 selectInput() 的新选择,而且接受新的选择数量?在此先感谢您的帮助。
菲利普
library(shiny)
ui <- fluidPage(
tabPanel("tbls",
selectInput("tab1",label="Pick a table:",choices=c("a","b","c")),
selectInput("cht1",label="Pick a time series:",choices=c("d","e","f"))
)
)
server <- function(input,output,session) {
Nchoices <- reactive({case_when(
input$tab1=="a" ~c("d","e","f"),
input$tab1=="b" ~c("g","h","i"),
input$tab1=="c" ~c("j","k","l","m") # adding one more choice breaks the code
)})
observe({updateSelectInput(session,"cht1",
label="Pick a time series:",choices=Nchoices(),selected=NULL)})
observe(print(Nchoices()))
}
shinyApp(ui, server)
尝试使用 switch
而不是 case_when
。此外,renderUI
可能会有用。试试这个
library(shiny)
ui <- fluidPage(
tabPanel("tbls",
selectInput("tab1",label="Pick a table:",choices=c("a","b","c")),
uiOutput("myselect")
#selectInput("cht1",label="Pick a time series:",choices=c("d","e","f"))
)
)
server <- function(input,output,session) {
Nchoices <- reactive({
switch(input$tab1,
"a" = c("d","e","f"),
"b" = c("g","h"),
"c" = c("j","k","l","m") # adding one more choice breaks the code
)
})
output$myselect <- renderUI({
req(input$tab1)
selectInput("cht1",label="Pick a time series:",choices=Nchoices())
})
observe(print(Nchoices()))
}
shinyApp(ui, server)
请注意,在 case_when
中,所有 RHS 值都必须是同一类型。不一致的类型将引发错误。