2016-09-06 6 views
0

私は、ユーザーからcsvファイルを取得してアップロードするアプリケーションを構築しようとしています。その後、データフレームの特定の列を埋めるテキストボックスを入力し、 「GO」ボタンをクリックすると、いくつかのスクリプトがバックグラウンドで実行され、ダウンロード可能なデータフレームが用意されています。問題は、全体的なリアクションアーキテクチャによって、ステップバイステップアルゴリズムを設定することが難しくなることです。あなたはそれを行うためのフレームワークを設定することで私を助けてくれますか? 1.負荷をクリックした後、他の入力ボックス3を埋めるためにユーザのユーザ2.待ちのcsvファイルからデータフレーム「行く:フロント私がしたいことを持っ今アップロードされたcsvファイルのスクリプトをshinyで実行する

shinyUI(fluidPage(
      titlePanel("Uploading Files"), 
      fileInput('file1', 'Choose file to upload', 
         accept = c('text/csv', 
         'text/comma-separated-values', 
         'text/tab-separated-values', 
         'text/plain','.csv','.tsv')),   
      dateInput('date',"Select when the file was uploaded", 
         value = NULL, 
         format = 'yyyy-mm-dd'), 
      textInput('text1','Type what will be in column 6'), 
      textInput('text2','Type what will be in column 7'), 
      actionButton('go','go), 
      tableOutput('readytable') 

を次のなど理想的にはそれが見えます'ユーザーが例えばdf$column6 <- input$text1のように挿入した入力を持つデータフレーム上の関数群を実行し、その後にもう一度csvファイルとして書き込まれる準備ができているデータフレームが残っています。

答えて

1

反応変数を使用して光沢のある反応性を制御することができます。あなたの問題の例を以下に示します。ダウンロードボタンはRStudioビューアでは機能しませんので、ダウンロードボタンを使用するにはブラウザでアプリを起動してください。

library(shiny) 
runApp(list(
    ui = shinyUI(pageWithSidebar(
    headerPanel('Uploading Files'), 
    sidebarPanel( 
      fileInput('file1', 'Choose file to upload', 
         accept = c('text/csv', 
         'text/comma-separated-values', 
         'text/tab-separated-values', 
         'text/plain','.csv','.tsv')), 
    uiOutput('buttonsUI'), br(), 
    uiOutput('downloadUI') 
), 
    mainPanel(
    tableOutput('readytable') 
) 
)), 
    server = shinyServer(function(input, output) { 
    # variables to control the sequence of processes 
    controlVar <- reactiveValues(fileReady = FALSE, tableReady = FALSE) 
    # to keep the data upload 
    dat <- NULL 
    # handle the file reading 
    observeEvent(input$file1, { 
     controlVar$fileReady <- FALSE 
     if (is.null(input$file1)) 
     return() 
     inFile <- input$file1 
     dat <<- read.csv(inFile$datapath) 
     if(!is.data.frame(dat)) 
     return() 
     controlVar$fileReady <- TRUE 
    }) 
    # show buttons only when file is uploaded 
    output$buttonsUI <- renderUI({ 
     if (controlVar$fileReady) 
     div(
      dateInput('date','Select when the file was uploaded', 
         value = NULL, 
         format = 'yyyy-mm-dd'), 
      textInput('text1','Type what will be in column 6'), 
      textInput('text2','Type what will be in column 7'), 
      actionButton('go','go') 

     ) 
    }) 
    # show a download button only if data is ready 
    output$downloadUI <- renderUI({ 
     if (controlVar$tableReady) 
      downloadButton('downloadData', 'Download') 
    }) 
    # add columns to dat and run some script on it 
    observeEvent(input$go, { 
     controlVar$tableReady <- FALSE 
     if (!is.null(input$text1)) 
     dat$column6 <<- input$text1 
     if (!is.null(input$text2)) 
     dat$column7 <<- input$text2 
     # simulate running a cool script on dat 
     Sys.sleep(2) 
     controlVar$tableReady <- TRUE 
    }) 
    # render table after uploading file or running the script 
    output$readytable <- renderTable({ 
     input$go 
     if (controlVar$fileReady || controlVar$tableReady) 
     dat 
    }) 
    # handle the download button 
    output$downloadData <- downloadHandler(
     filename = function() { 'newData.csv' }, 
     content = function(file) { 
     write.csv(dat, file) 
     } 
    ) 
    }) 
)) 
関連する問題