R shiny:输入数据后获取所有因素

R shiny: get all Factors after input Data

嘿,我有一个 Shiny App,它可以输入数据并使用它们。但现在我想要一个动作按钮,它 select 子集功能的因素。问题是,当我启动应用程序并输入数据时,因子不存在,在第一次单击操作按钮后,因子获取数据的列名称。

我该如何编程,以便在读取数据后直接向我显示这些因素。感谢您的帮助!

这里是UI和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")

服务器:

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
  })

您可以将代码放在 observeEvent for input$choose 中,而不是放在 observe 中。像这样:

  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)]
      )
    }
  })

希望对您有所帮助!