简体   繁体   中英

Dynamically update modal box in shiny

I have a simple application, where when the user uploads excel file (having sheets names like "s", "a", "e" etc), the list of sheets names should appear in the select drop down, but I see some error here as shown below.

library(shiny)

ui <- fluidPage(
 # fileInput("file1",label = "File 1", accept = c('xlsx')),
 actionButton("act", "Submit"),

)

server <- function(input, output, session) {

observeEvent(input$act, {
  showModal(modalDialog(fileInput("file2",label = "File 2", accept = c('xlsx')),
                        selectInput("sel", "sheets", choices = excel_sheets(path = input$file2$datapath), selected = "s")))
})

}

shinyApp(ui, server)

When I run the app and click "Submit", I get below error

Warning: Error in : `path` must be a string
  1: runApp

You need to build the selectInput dynamically with a uiOutput , checking if a file has already been loaded

library(shiny)
library(readxl)

ui <- fluidPage(
  actionButton("act", "Submit"),
  
)

server <- function(input, output, session) {
  
  output$select_sheets <- renderUI({
    if (!is.null(input$file2))
      selectInput("sel", "sheets", 
      choices = excel_sheets(input$file2$datapath), 
      selected = "s")
    })
  
  observeEvent(input$act, {
    showModal(
      modalDialog(
        fileInput("file2",label = "File 2", accept = c('xlsx')), 
        uiOutput("select_sheets")
      )
    )
  })
  
}

shinyApp(ui, server)

Created on 2022-11-15 with reprex v2.0.2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM