将用户输入传输到rshiny中的csv文件

时间:2017-07-19 04:17:06

标签: r csv shiny

在SHINY中,我们如何将用户输入从一些文本和数字框转移到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

1 个答案:

答案 0 :(得分:1)

您可以将数据存储为反应式表达式中的数据框,并使用下载按钮和下载处理程序下载数据。

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

希望这有帮助!

相关问题