gpt4 book ai didi

r - Shiny :下载 zip 存档

转载 作者:行者123 更新时间:2023-12-04 02:05:02 34 4
gpt4 key购买 nike

我做不出 Shiny 的 downloadHandler输出 zip 文件:

# server.R
library(shiny)

shinyServer(function(input, output) {
output$downloadData <- downloadHandler(
filename <- function() {
paste("output", "zip", sep=".")
},

content <- function(fname) {
fs <- c()
tmpdir <- tempdir()
setwd(tempdir())
for (i in c(1,2,3,4,5)) {
path <- paste0("sample_", i, ".csv")
fs <- c(fs, path)
write(i*2, path)
}
zip(zipfile=fname, files=fs)
}
)
})

和简单的 ui.R :
shinyUI(fluidPage(
titlePanel(""),
sidebarLayout(
sidebarPanel(
downloadButton("downloadData", label = "Download")
),
mainPanel(h6("Sample download", align = "center"))
)
))

我有很好的输出,除了错误:
> shiny::runApp('C:/Users/user/AppData/Local/Temp/test')

Listening on http://127.0.0.1:7280
adding: sample_1.csv (stored 0%)
adding: sample_2.csv (stored 0%)
adding: sample_3.csv (stored 0%)
adding: sample_4.csv (stored 0%)
adding: sample_5.csv (stored 0%)
Error opening file: 2
Error reading: 6

并且没有保存对话框来保存存档。但是在 temp文件夹中显示了正确的存档。如何正确共享存档?

最佳答案

您正在使用 <-在 downloadHandler 函数内部,应该使用 = .您也可能需要定义 contentType :

library(shiny)

runApp(
list(server = function(input, output) {
output$downloadData <- downloadHandler(
filename = function() {
paste("output", "zip", sep=".")
},
content = function(fname) {
fs <- c()
tmpdir <- tempdir()
setwd(tempdir())
for (i in c(1,2,3,4,5)) {
path <- paste0("sample_", i, ".csv")
fs <- c(fs, path)
write(i*2, path)
}
zip(zipfile=fname, files=fs)
},
contentType = "application/zip"
)
}
, ui = fluidPage(
titlePanel(""),
sidebarLayout(
sidebarPanel(
downloadButton("downloadData", label = "Download")
),
mainPanel(h6("Sample download", align = "center"))
)
))
)

关于r - Shiny :下载 zip 存档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26881368/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com