我正在尝试使用前一个selectInput中的值更新selecInput中的选项,但我无法让观察者正常工作。
我尝试使用observe、observeEvent和不使用它,但是选项没有更新。
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("select_data", label = "Select Data", choices = c(" ", "islands", "iris", "mtcars")),
selectInput("select_var", label = "Select Variable", choices = "" )
),
mainPanel(
tableOutput("table")
)
)
)
server <- function(input, output, session) {
observeEvent(input$select_data,{
updateSelectInput(session, "select_var", choices = names(input$select_data))
})
varnames <- reactive({
input$select_data
})
output$table <- renderTable({
head(varnames[[input$select_var]])
})
}
shinyApp(ui, server)发布于 2021-09-10 14:31:48
您可以将数据存储在命名列表中,您可以从该列表中根据下拉列表进行选择。
library(shiny)
list_data <- tibble::lst(islands, iris, mtcars)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("select_data", label = "Select Data", choices = c(" ", names(list_data))),
selectInput("select_var", label = "Select Variable", choices = "")
),
mainPanel(
tableOutput("table")
)
)
)
server <- function(input, output, session) {
observeEvent(input$select_data,{
updateSelectInput(session, "select_var", choices = names(list_data[[input$select_data]]))
})
output$table <- renderTable({
if(input$select_data != '' && input$select_var != '')
head(list_data[[input$select_data]][input$select_var])
})
}
shinyApp(ui, server)https://stackoverflow.com/questions/69133343
复制相似问题