使用Shiny将文件从服务器下载到本地目录

时间:2018-01-05 21:50:34

标签: r shiny

我不确定我是否正确理解这一点。我要下载的数据集应该放在RStudio服务器中的哪个位置?我还不想部署,所以我没有使用Shiny服务器,但我想使用下载功能并将数据集保存为本地目录中的文件。

我使用Shiny fileInput上传了一个文件,并使用copy.file()保存了该文件。我被告知上传的文件已保存到服务器。该文件名为0.tsv。之后,我读了那个文件并下载了。 GUI工作,但文件不会写入文件并保存到本地目录。我的问题是:我的方法中是否有任何遗漏的东西?

library(shiny)

ui <- fluidPage(
  titlePanel('File download'),
  sidebarLayout(
    sidebarPanel(
      selectInput("dataset", "Choose a dataset:", 
                  choices = c("data")),
      radioButtons("filetype", "File type:",
                   choices = c( "tsv")),
      downloadButton('downloadData', 'Download')
    ),
    mainPanel(
      tableOutput('table')
    )
  )
)



function(input, output) {
  datasetInput <- reactive({
    # Fetch the appropriate data object, depending on the value
    # of input$dataset.
    data <- read.table("home/user/0.tsv")
    switch(input$dataset,
           "data" = data)
  })

  output$table <- renderTable({
    datasetInput()
  })

  # downloadHandler() takes two arguments, both functions.
  # The content function is passed a filename as an argument, and
  #   it should write out data to that filename.
  output$downloadData <- downloadHandler(

    # This function returns a string which tells the client
    # browser what name to use when saving the file.
    filename = function() {
      paste(input$dataset, input$filetype, sep = ".")
    },

    # This function should write data to a file given to it by
    # the argument 'file'.
    content = function(file) {
      sep <- switch(input$filetype, "tsv" = "\t")

      # Write to a file specified by the 'file' argument
      write.table(datasetInput(), file, sep = sep,
                  row.names = FALSE)
    }
  )
}


shinyApp(ui = ui, server = server)

    ui <- fluidPage(
      titlePanel('File download'),
      sidebarLayout(
        sidebarPanel(
          selectInput("dataset", "Choose a dataset:", 
                      choices = c("data")),
          radioButtons("filetype", "File type:",
                       choices = c( "tsv")),
          downloadButton('downloadData', 'Download')
        ),
        mainPanel(
          tableOutput('table')
        )
      )
    )



    function(input, output) {
      datasetInput <- reactive({
        # Fetch the appropriate data object, depending on the value
        # of input$dataset.
        data <- read.table("home/user/0.tsv")
        switch(input$dataset,
               "data" = data)
      })

      output$table <- renderTable({
        datasetInput()
      })

      # downloadHandler() takes two arguments, both functions.
      # The content function is passed a filename as an argument, and
      #   it should write out data to that filename.
      output$downloadData <- downloadHandler(

        # This function returns a string which tells the client
        # browser what name to use when saving the file.
        filename = function() {
          paste(input$dataset, input$filetype, sep = ".")
        },

        # This function should write data to a file given to it by
        # the argument 'file'.
        content = function(file) {
          sep <- switch(input$filetype, "tsv" = "\t")

          # Write to a file specified by the 'file' argument
          write.table(datasetInput(), file, sep = sep,
                      row.names = FALSE)
        }
      )
    }


    shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:0)

非常感谢您的回复。以下是正确的方法。这很好。

server <- function(input, output) {

  # Reactive value for selected dataset ----
  datasetInput <- reactive({
    switch(input$dataset,
           "params" = params)
  })

  # Table of selected dataset ----
  output$table <- renderTable({ 
    datasetInput()
  })

  # Downloadable csv of selected dataset ----
  output$downloadData <- downloadHandler(
    filename = function() {
      paste(input$dataset, ".csv", sep = "")
    },
    content = function(file) {
      write.table(datasetInput(), file, row.names = FALSE)
    }
  )

}