R有光泽:输入数据后得到所有因子

时间:2017-11-11 11:11:43

标签: r shiny shiny-server

嘿,我有一个闪亮的应用程序,它输入数据并与它们一起工作。但是现在我想要一个动作按钮来选择子集funktion的因子。问题是,当我启动应用程序并输入数据时,因子不存在,只需在第一次单击操作按钮后,因子就会得到数据的列名称。

如何编程,以便在读取数据后直接向我显示因子。谢谢你的帮助!

这是UI和服务器的一部分

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

1 个答案:

答案 0 :(得分:0)

您可以将其放入观察中,而不是将代码放在observeEvent input$choose中。像这样:

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

希望它有所帮助!