无法在downloadHandler

时间:2018-06-26 01:58:04

标签: r shiny r-markdown flexdashboard

我正在尝试将源信息添加到包含数据帧的下载表中。我正在使用下载文件处理程序来下载文件。我已经实现了仅下载数据框而不附加其他文本的方式,并且工作正常。尝试在表格中添加文本时,我得到了csv格式的下载文件,但是缺少源信息,最后一行的第一列和第二列分别为NA。最后一行的其余部分为空。

这是我的下载处理程序代码,不会产生附加的文本输出:

downloadHandler(
filename = function() {
paste("Estimation_", Sys.Date(), ".csv", sep="")
},
content = function(file) {
  df <- localTable()
  df[nrow(df)+1,] <- NA    

  df[nrow(df),1] <- "Source: This is the source information for the table"
  write.csv(df, 
          file,
          na = "",
          row.names=FALSE,
          append = TRUE)
},
contentType = "text/csv",
outputArgs = list(label = "Download Table")
)

另一方面,我分别尝试了相同的功能,效果很好。这是我在一个单独的R脚本中执行相同功能的代码。

df = data.frame(hello=rnorm(1:5), world=rnorm(1:5));
df[nrow(df)+1,] <- NA
df[nrow(df),1] <- "Source: This is the source information for the table"
write.csv(df, 
      file ="abc.csv",
      na = "",
      row.names=FALSE)

我不知道问题出在特别是Shiny还是其他问题。请注意,我将此代码用作运行RMD文件以生成输出的Flexdashboard的一部分。 我尝试了其他几种创建数据框的方法,如下所示;然后传递给downloadHandler但没有运气。

dlTable <- reactive({
    df_results <- localTable()
   df_notes <- "Source: This is the source information for the table"
   df_to_dl <- rbindlist(list(df_results,df_notes))
   return(df_to_dl)
})

任何建议和帮助都将受到高度赞赏。

谢谢

0 个答案:

没有答案