【问题标题】:Save dataframe in Shiny to access it later and download it?将数据框保存在 Shiny 中以便以后访问并下载?
【发布时间】:2018-06-01 10:05:45
【问题描述】:

我是闪亮的新手。 我有一个非常基本的问题,但我在 stackoverflow 上找不到解决方案。
我正在使用 wleepang (https://github.com/wleepang/shiny-directory-input) 创建的目录输入函数。 我写了一个函数read_filesrbind 所选目录中的所有文件。 我可以用 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 目录中?

我的 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.您可以使用save将数据保存为R object,然后使用load读取数据,2.下载作为 xlxs 调查this
  • @parth 我已经使用反应函数 upload_data &lt;- eventReactive(input$upload, { read_files(readDirectoryInput(session, 'directory')) }) 保存了元素,并且可以通过 upload_data() 访问它,这对我有用
  • @parth save 在闪亮时不起作用,或者你能给我一个代码示例吗?谢谢

标签: r shiny


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-10
    • 2020-09-21
    • 2019-11-21
    • 1970-01-01
    • 2020-08-07
    • 2022-11-02
    • 1970-01-01
    • 2021-08-02
    相关资源
    最近更新 更多