我读过类似的关于堆栈溢出的问题,但似乎无法让我的代码正确工作。我已经把这个简化成了重装。
我试图允许用户选择一个表,并在该表的基础上选择一个与所选表相关联的列。
我看到的一个问题是,当应用程序启动时,无法立即填充列选择器(在从表选择器中选择表之前)。而且,不管我做什么,列选择器都不会填充列。谢谢你能帮我的忙。
library(shiny)
tables <- c("A", "B", "C")
columns <- list(A = c("a1", "a2", "a3"), B = c("b1", "b2"), C = c("c1", "c2", "c3", "c4"))
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput(inputId = "table_selector", label = "Tables", choices = tables, selected = "A"),
selectInput(inputId = "column_selector", label = "Columns", choices = "")),
# Show a plot of the generated distribution
mainPanel(
textOutput("selected_table"),
textOutput("selected_columns")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
rv <- reactiveValues("selected_cols" = unlist(columns$A))
observeEvent(input$selected_table_selector, {
message("Table event observed")
rv$selected_cols <- selected_table[input$table_selector()]
updateSelectInput("column_selector", choices = rv$selected_cols())
})
# Will use the following two query a database
output$selected_table <- renderText({paste(input$table_selector)})
output$selected_columns <- renderText({rv$selected_cols})
}
# Run the application
shinyApp(ui = ui, server = server)发布于 2022-01-21 23:03:14
您有许多语法问题。尝尝这个
library(shiny)
tables <- c("A", "B", "C")
columns <- list(A = c("a1", "a2", "a3"), B = c("b1", "b2"), C = c("c1", "c2", "c3", "c4"))
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput(inputId = "table_selector", label = "Tables", choices = tables, selected = "A"),
selectInput(inputId = "column_selector", label = "Columns", choices = "")),
# Show a plot of the generated distribution
mainPanel(
textOutput("selected_table"),
verbatimTextOutput("selected_columns")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output,session) {
#rv <- reactiveValues(selected_cols = NULL)
observeEvent(input$table_selector, {
message("Table event observed")
choices <- columns[[input$table_selector]]
updateSelectInput(session, "column_selector", choices = choices )
})
# Will use the following two query a database
output$selected_table <- renderText({paste(input$table_selector)})
output$selected_columns <- renderPrint({input$column_selector})
}
# Run the application
shinyApp(ui = ui, server = server)https://stackoverflow.com/questions/70808281
复制相似问题