2017-11-11 7 views
0

ちょっと私は、データを入力してそれらを使用するシャイニーアプリケーションを持っています。しかし、今私はサブファンクションのファクターを選択するアクションボタンが必要です。問題は、私がアプリケーションを起動してデータを入力すると、要素がそこになく、アクションボタンを最初にクリックした直後に要素にデータの列名が得られることです。R shiny:入力後にすべての要因を取得

データを読み込んだ直後に要因が表示されるように、これをプログラムするにはどうすればよいですか。ご協力ありがとうございました!ここで

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

サーバー:代わりにinput$chooseためobserveEventにコードを置くの

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

あなたは観察中にそれを置くことができます。このようなもの:

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

関連する問題