Search code examples
rshinydashboard

Shiny dashboard R: Using headers excel file as input SelectInput field


Currently I am working on, I suppose a very simple Shiny dashboard, but since I am rather new to it, I still do not get it working. I would like to create a dashboard where the user can select a file. After selecting a file, the names of the headers of this file have to be used as input for a SelectInput field.

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput('datafile', 'Choose xlsx file',
                accept = c(".xlsx")
      ),
      selectInput("vars", "Select a variable:", choices=names("varselect"),
                  multiple = TRUE)    
    ),
    mainPanel(


    )
  )
)

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

  Dataset <- reactive({
    infile <- input$datafile
    if (is.null(infile)) {
      return(data.frame())
    }
    read.xlsx(infile$datapath, 
              sheetIndex = 1) 

  })

  output$varselect <- renderUI({

  })
  observe({
    if (identical(Dataset(), '') || identical(Dataset(), data.frame()))


      updateSelectInput(session, inputId="vars", label="Variables to use:",
                        choices=names(Dataset()), selected=names(Dataset()))
  })
  }



shinyApp(ui = ui, server = server)

Could somebody help me to explain what I am doing wrong/ how I can most efficiently tackle this problem?

Thank you in advance!


Solution

  • You are completely right, after removing it it is working. Thank you!

    ui <- fluidPage(   sidebarLayout(
        sidebarPanel(
          fileInput('datafile', 'Choose xlsx file',
                    accept = c(".xlsx")
          ),
          selectInput("vars", "Select a variable:", choices=names("varselect"),
                      multiple = TRUE)    
        ),
        mainPanel(
    
    
        )   ) )
    
    server <- function(input, output,session) {
         Dataset <- reactive({
        infile <- input$datafile
        if (is.null(infile)) {
          return(data.frame())
        }
        read.xlsx(infile$datapath, 
                  sheetIndex = 1) 
           })
         output$varselect <- renderUI({
           })   observe({
    
    
    
        updateSelectInput(session, inputId="vars", label="Variables to use:",
                            choices=names(Dataset()), selected=names(Dataset()))   }) }
    
    
    
    shinyApp(ui = ui, server = server)