Update Shiny's 'selectInput' dropdown with new values after uploading new data using fileInput

后端 未结 2 522
小鲜肉
小鲜肉 2020-12-19 12:11

I have a Shiny app that includes a number of dropdown selection boxes, the values of which are filled from reading an RDS file. The app also includes a fileInput function to

2条回答
  •  别那么骄傲
    2020-12-19 12:58

    I added reactive object myData that you have to use for table contents, but more importantly to update choices in selectInput (check observe and updateSelectInput part).

    library(shiny)
    
    ui <- shinyUI(
        fluidPage(
            fileInput("file1", "Choose file to upload", accept = ".rds"),
            selectInput("myNames","Names", ""),
            tableOutput("contents")
        )
    )
    
    server <- function(input, output, session) {
    
        myData <- reactive({
            inFile <- input$file1
            if (is.null(inFile)) {
                d <- myDataFrame
            } else {
                d <- readRDS(inFile$datapath)
            }
            d
        })
    
        output$contents <- renderTable({
            myData()
        })
    
        observe({
             updateSelectInput(session, "myNames",
                               label = "myNames",
                               choices = myData()$names,
                               selected = myData()$names[1])
        })
    
    }
    
    shinyApp(ui, server)
    

提交回复
热议问题