2017-07-19 14 views
1

在SHINY中,我們如何將某些文本和數字框的用戶輸入轉換爲CSV文件?在rshiny中將用戶輸入轉換爲csv文件

的處理流程將是:

- First the users input the information into those text boxes. 
- Then the users press a Run button 
- Upon pressing the button, a CSV file will be generated containing the information from those text boxes 
+0

獲取所有輸入字段並構造csv? –

+0

是@RomanLuštrik,但我不知道該怎麼處理 –

回答

1

可以將數據存儲爲在反應中表達的數據幀,並使用downloadbutton和downloadhandler下載數據。

server.R

library(shiny) 

shinyServer(function(input, output, session) { 

    dataReactive <- reactive({ 
data.frame(text = c(input$text1, input$text2, input$text3)) 

    }) 

    output$exampleTable <- DT::renderDataTable({ 
    dataReactive() 
    }) 

    output$downloadData <- downloadHandler(
    filename = function() { 
     paste("dataset-", Sys.Date(), ".csv", sep="") 
    }, 
    content = function(file) { 
     write.csv(dataReactive(), file) 

    }) 


}) 

ui.R:

shinyUI(fluidPage(

    sidebarLayout(
    sidebarPanel(
     textInput("text1","Text 1:",value="example text 1"), 
     textInput("text2","Text 2:",value="example text 2"), 
     textInput("text3","Text 3:",value="example text 3"), 
     downloadButton('downloadData', 'Download data') 

    ), 
    mainPanel(
       DT::dataTableOutput("exampleTable") 
    ) 
) 
)) 

希望這有助於!

+1

謝謝Florian!它非常有幫助,並且完美無缺。是否有另一個版本沒有下載按鈕,其中用戶沒有獲取CSV文件。我想知道,因爲我將它轉化爲圖元文件 –

+0

很高興我能提供幫助。您也可以使用actionbutton和observeEvent,而不是使用downloadbutton和downloadhandler,並使用write.csv將文件存儲在服務器上。 – Florian

+0

是這樣的嗎? UI ---- actionButton('action','Data')|| } }內容=「輸出$動作< - 觀察事件函數(文件)write.csv(dataReactive(),file) }) –