【发布时间】:2018-06-01 10:05:45
【问题描述】:
我是闪亮的新手。
我有一个非常基本的问题,但我在 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 目录中?
我的 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)
}
【问题讨论】:
-
@parth 我已经使用反应函数
upload_data <- eventReactive(input$upload, { read_files(readDirectoryInput(session, 'directory')) })保存了元素,并且可以通过upload_data()访问它,这对我有用 -
@parth save 在闪亮时不起作用,或者你能给我一个代码示例吗?谢谢