【问题标题】:Need to output a data file in a specific format using download handler in Rshiny需要使用 R Shiny 中的 downloadhandler 以特定格式输出数据文件
【发布时间】:2021-01-04 11:00:56
【问题描述】:

我有一个基本的闪亮应用程序,我希望用户选择一个数据集,然后能够在没有列名的情况下下载选定的数据集。另外,我想为正在下载的数据集添加页眉和页脚记录。

头记录 = 0, MYFILE, 20200917。

页脚记录 = 9,[所选数据集中的行数],[对所选数据集的第一列求和]。

下面是我目前的代码:

ui

library(shiny)

shinyUI(fluidPage(
  titlePanel('File download'),
  sidebarLayout(
    sidebarPanel(
      selectInput("dataset", "Choose a dataset:", 
                  choices = c("Rock", "Pressure", "Cars")),
      radioButtons("filetype", "File type:",
                   choices = c("csv", "tsv")),
      downloadButton('downloadData', 'Download')
    ),
    mainPanel(
      tableOutput('table')
    )
  )
))

服务器:

library(shiny)
library(tidyverse)

shinyServer(function(input, output) {
  datasetInput <- reactive({

    switch(input$dataset,
           "Rock" = rock,
           "Pressure" = pressure,
           "Cars" = cars)
  })
  
  output$table <- renderTable({
    datasetInput()
  })
  

  output$downloadData <- downloadHandler(
    
    filename = function() {
      paste("myfile", input$dataset, gsub("-", "", Sys.Date()), "_01_res.dat_00000002", sep = ".")
    },
    
    content = function(file) {
      # Write to a file specified by the 'file' argument
      write.table(datasetInput(), file,
                  row.names = FALSE, col.names = F, sep = ",")

    }
  )
})

到目前为止,上面的代码可以工作,但没有页眉和页脚记录。当我尝试将页眉和页脚记录添加到数据集时,它不起作用。

我只需要更改 downloadHandler 部分来添加页脚和页眉记录,但是当我这样做时会出现错误。以下是我尝试过的:

  output$downloadData <- downloadHandler(
    
    filename = function() {
      paste("myfile", input$dataset, gsub("-", "", Sys.Date()),"_01_res.dat_00000002", sep = ".")
    },
    
    content = function(file) {
      # Write to a file specified by the 'file' argument
      a = paste0("0,","MYFILE,",gsub("-", "", Sys.Date()))
      z = paste0("9,",nrow(datasetInput), ",", sum(datasetInput))
      write.table(x = a, quote = F, sep = ",", col.names = F, row.names = F, file, sep = sep)
      write.table(x = datasetInput(), quote = F, sep = ",", col.names = F, row.names = F, append = T, file, sep = sep)
      write.table(x = z, quote = F, sep = ",", col.names = F, row.names = F, append = T, file, sep = sep)


    }
  )

我可以在没有 Shiny 的普通 R 文件中实现这一点。我这样做的方式是编写一个函数,然后调用该函数,如下所示:

功能

write_output <- function(a, df, z){
  filename = paste("myfile", "Rock", gsub("-", "", Sys.Date()) ,"_01_res.dat_00000002",sep = "") %>% gsub( "-", "", .)
  write.table(x = a, file = filename, quote = F, sep = ",", col.names = F, row.names = F)
  write.table(x = df, file = filename, quote = F, sep = ",", col.names = F, row.names = F, append = T)
  write.table(x = z, file = filename, quote = F, sep = ",", col.names = F, row.names = F, append = T)
}

应用函数:

write_output(a =  paste0("0,","MyFILE,",gsub("-", "", Sys.Date())), 
             df = datasetInput, 
             z = paste0("9,",nrow(datasetInput), ",", sum(datasetInput[,1])))

所以,简而言之,我需要输出一个带有与数据集大小不同的页眉和页脚记录的数据集。

这是一个输出示例 - 第一行是标题记录,接下来是来自 rock 数据集的 3 行,最后一行是页脚

0,我的文件,20200917

4990,2791.9,0.0903296,6.3

7002,3892.6,0.148622,6.3

7558,3930.66,0.183312,6.3

9,48,345011

任何帮助将不胜感激:)。谢谢

【问题讨论】:

  • 你得到什么错误?
  • 你的代码在计算 z 时有一个错误,而不是 z = paste0("9,",nrow(datasetInput()), ",", sum(datasetInput()[,1]))

标签: r shiny shiny-server


【解决方案1】:

我的代码中有多个分隔符,这让我感到困惑。我拿出其中一个论点,它奏效了。这是下载处理程序的新代码。

output$downloadData

filename = function() {
  paste("myfile", input$dataset, gsub("-", "", Sys.Date()), sep = ".")
},

content = function(file) {
  # Write to a file specified by the 'file' argument
  a = paste0("0,","MYFILE,",gsub("-", "", Sys.Date()))
  z = paste0("9,",nrow(datasetInput()), ",", sum(datasetInput()[,1]))
  write.table(x = a, quote = F, sep = ",", col.names = F, row.names = F, file)
  write.table(x = datasetInput(), quote = F, sep = ",", col.names = F, row.names = F, append = T, file)
  write.table(x = z, quote = F, sep = ",", col.names = F, row.names = F, append = T, file)
  

}

)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-28
    • 2020-12-08
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    • 2017-08-13
    相关资源
    最近更新 更多