在Shiny中保存数据框以便以后访问并下载它?

时间:2018-06-01 10:05:45

标签: r shiny

我是新生的闪亮之作。 我有一个非常基本的问题,但我无法在stackoverflow上找到解决方案 我正在使用wleepang(https://github.com/wleepang/shiny-directory-input)创建的目录输入函数。 我写了一个函数read_files rbind 所选目录中的所有文件。 我可以使用 renderTable 显示此表,这非常有效。但我无法保存此表以便以后使用(检查缺少的数据,添加列,绘制ggplots ..)并下载 write.xlsx

ui <- fluidPage(
directoryInput('directory', label = 'select a directory'),
actionButton("upload", label="Hochladen"),
downloadButton("download", label="Runterladen")
)  

server <- function(input, output, session) {
#this part is to set the directory
 observeEvent(
 ignoreNULL = TRUE,
 eventExpr = {
   input$directory
 },
 handlerExpr = {
   if (input$directory > 0) {
  path = choose.dir(default = readDirectoryInput(session, 'directory'))
  updateDirectoryInput(session, 'directory', value = path)
  }})
#now comes the actual code

  observeEvent(input$upload,{
 df <- read_files(readDirectoryInput(session, 'directory'))
})

我以后如何访问此df?

output$downloadData <- downloadHandler(
   filename = function() { 
     paste('tabelle', '.csv', sep="") },
   content = function(file) {
     write.xlsx(df, file)
   }
 )
}

我的第二个问题如何将它作为xlsx文件下载到set目录中?

my global.r,read_files函数

source('directoryInput.R')
read_files = function(inDir, pat="*.csv", readMe=read.csv2){
files = list.files(inDir, pattern=pat)
files = lapply(files, function(x) file.path(inDir, x))
df = do.call(rbind, lapply(files, readMe))
return(df)
}

1 个答案:

答案 0 :(得分:0)

我使用反应函数保存了元素

upload_data <- eventReactive(input$upload, {      
read_files(readDirectoryInput(session, 'directory')) }) 

并且可以通过upload_data()访问它,这对我有用

相关问题