如何使用R Shiny从Box URL(而不是从服务器)下载文件



我有一个Excel(.xslx(文件存储在Box位置,具有固定的URL。如何用R Shiny编写代码,将此Excel文件下载到服务器中以供进一步操作和使用?到目前为止,我发现的所有例子要么是下载服务器上已经存在的文件,要么是将URL上显示的表下载到.csv文件中。downloadHandler似乎没有此功能。以下是我们的想法,但不确定如何进一步进行。谢谢

library(shiny)
boxURL <- "https://*****.box.com/s/*****************"
app <- list(
ui = fluidPage(
titlePanel(""),
sidebarLayout(
sidebarPanel(
downloadButton("downloadFile", label = "Download Excel from Box URL")
),
mainPanel(h6("Table In Downloaded Excel File", align = "center"))
)
),
server = function(input, output) {  
output$downloadFile<- downloadHandler(
filename <- function() {
#code to download Excel file to filename
...
},
content <- function(file) {
# the content is a typical table in Excel.
},
contentType = "excel"
)
}
)

为了从https://www.box.com,您可能需要身份验证。通常,对于此类用例,最好使用客户端或接口。谷歌快速搜索显示了以下软件包,该软件包为box.com API提供了R接口:

https://cran.r-project.org/web/packages/boxr/vignettes/boxr.html.

使用HTTP GET请求和write_disk()函数在下载处理程序中传递内容是可行的。

在您的服务器回调中:

output$downloadFile <- downloadHandler(
filename = function() {
# code to define filename
paste0("filename", ".xlsx")
},
content = function(file) {
# Perform a HTTP GET request to your Box URL and download the 
# resulting file to a file named "filename.xlsx"
GET(boxURL, write_disk(file))
},
contentType = "excel"
)

请注意,只有当您的URL指向文件下载,而不是显示文件的Box页面时,这才有效。这意味着,如果你在浏览器上访问该URL,它应该会提示下载你想要的文件。

最新更新