R闪亮,对输入的数据应用已实现的功能

时间:2019-07-09 14:19:51

标签: r dataframe input shiny

我想实现一个应用程序,该应用程序将.csv文件作为输入(用户从他的计算机中选择文件),然后我想将此文件作为已实现的函数“ f”中的参数先前。

我可以给你ui.R和server.R

ui.R

library(shiny)

shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput("file",
                "Put your portfolio data here",
                accept=c(".csv")
                )
    ),

    mainPanel(tableOutput("table"))
  )

))

server.R

library(shiny)



shinyServer(function(input, output) {

    data <- reactive({
      file1 <- input$file
      if(is.null(file1)){return()} 
      read.csv2(file=file1$datapath)
    })


    #to view the data    
    output$table <- renderTable({
      if(is.null(data())){return ()}
      data()
    })

})

最后,我的函数“ f”应该提供一个.csv文件作为输出。

1 个答案:

答案 0 :(得分:0)

ui.R

library(shiny)

shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput("file",
                "Put your portfolio data here",
                accept=c(".csv")
                ),
      downloadButton(download_data.csv, "Download File")
    ),

    mainPanel(tableOutput("table"))
  )

))

server.R

shinyServer(function(input, output) {

    data <- reactive({
      file1 <- input$file
      if(is.null(file1)){return()} 
      read.csv2(file=file1$datapath)
    })


    #to view the data    
    output$table <- renderTable({
      if(is.null(data())){return ()}
      data()
    })

    # to download the tadata
    output$download_data.csv <- downloadHandler(

    filename = "download_data.csv",
    content = function(file){

      data <- data() # At this point you should have done the transformation 
                     # to the data

      write.csv(data, file, row.names = F)

    }

  )


})