Select xls sheet with read_xls on Shinny

Hi,

I pretty new on Shiny App. When I choose my data from an excel file, I would like to select the sheet and to show it on the App.
I use reactive to show and select on SelectInput the name of the datasheet but when I change the select sheet, it appears 1s and go back to the 1st sheet. What is wrong in my code?
Thank you

library(shiny)
library(readxl)

ui <- fluidPage(
  fileInput('file1', 'Input'),
  selectInput('sheet', 'Afficher sheet', ""),
  tableOutput('table')
)

server <- function(input, output, session) {
  data <- reactive({ 
    req(input$file1) ## ?req #  require that the input is available
    inFile <- input$file1
    updateSelectInput(session, inputId = 'sheet',
                      choices = excel_sheets(inFile$datapath)
                      )
    my_data <- read_excel(inFile$datapath, sheet = input$sheet)
    return (my_data)
  })
  output$table <- renderTable({
    data()
  })
}


shinyApp(ui, server)

Welcome to the community @Helene_Fr! Try pulling the updateSelectInput out of the reactive and putting it in an observeEvent triggered on input$file1. I believe this will result in the desired functionality.

server <- function(input, output, session) {
  data = reactive({
    req(input$file1)
    read_excel(input$file1$datapath, sheet = input$sheet)
  })
  
  observeEvent(input$file1, {
    updateSelectInput(session, 
                      inputId = 'sheet',
                      choices = excel_sheets(input$file1$datapath)
                      )
    })
  
  output$table <- renderTable({
    data()
  })
}
1 Like

It works perfectly thank you !

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.