0
votes

Hey I have a Shiny App which input Data and work with them. But now I want an actionbutton which select the Factors for a subset funktion. Problem is that when I start the App and input the Data, the Factors are not there, just after the first click on the action button the factors get the column names of the Data.

How can I program this so that the factors are displayed to me directly after reading the data. Thank you for your help!

Here a part of the UI and Server

              radioButtons(
                  "fileType_Input",
                  label = h5("Choose file type and click on browse"),
                  choices = list(".csv" = 1, ".xlsx" = 2),
                  selected = 1,
                  inline = TRUE
                ),

                fileInput('file1', ''  ),

                selectInput("letters", label=NULL, factors, multiple = TRUE),

                actionButton("choose", "Auswahl"),

                verbatimTextOutput("list")

Server:

shinyServer(function(input, output, session) {

  # Get the upload file
  myData <- reactive({
    inFile <- input$file1

    if (is.null(inFile)) {
      return(NULL) }

    if (input$fileType_Input == "1") {
      read.csv2(inFile$datapath,
                 header = TRUE,
                 stringsAsFactors = FALSE)
    } else {
      read_excel(inFile$datapath)
    }
  })

  # When the Choose button is clicked, add the current letters to v$choices
  # and update the selector
  observeEvent(input$choose, {
    data <- myData()
    factors <- colnames(data)   # get the names of the Factors in a Vector to select them
    v$choices <- input$letters  # append(v$choices,input$letters)
    updateSelectInput(session, "letters",
                      choices = factors #[!factors %in% v$choices)]
    )
  })

  output$list <- renderPrint({
    v$choices
  })
1

1 Answers

0
votes

Instead of putting your code in observeEvent for input$choose you could put it in observe. Something like this:

  observe({

    if(!is.null(myData()))
    {
      data <- myData()
      factors <- colnames(data)   # get the names of the Factors in a Vector to select them
      v$choices <- input$letters  # append(v$choices,input$letters)
      updateSelectInput(session, "letters",
                        choices = factors #[!factors %in% v$choices)]
      )
    }
  })

Hope it helps!