2017-11-11 148 views
0

嘿,我有一個閃亮的應用程序輸入數據並與他們一起工作。但是現在我想要一個爲子集功能選擇因子的動作按鈕。問題是,當我啓動應用程序並輸入數據時,因素不存在,只需在第一次點擊操作按鈕後,因素就會獲得數據的列名稱。R閃亮:得到輸入數據後的所有因素

我該如何編程,以便在讀取數據後直接顯示這些因子。感謝您的幫助!

這裏的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 
    }) 

回答

0

而不是把你的代碼observeEventinput$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)] 
    ) 
    } 
    }) 

希望它有幫助!

+0

感謝您的幫助,不幸的是,它不像我想象的那樣工作。我想保留「observevent」並且只安裝一個函數,我在數據加載之後,一旦「factor」矢量設置完成,所以他可以直接顯示給我。如果按照您的建議,我的按鈕將不再工作。 – Zorro

+0

然後,您可以在反應表達式中使用'updateSelectInput',而不是將它放在觀察中。 – SBista

+0

Sry,但我不明白你的想法...我嘗試了一些被動的表情,但我仍然有辦法。你能再解釋一遍嗎? – Zorro