Hello,
I am trying to convert a web scraping R script that I wrote into a Shiny app that others can use. Basically, I am using the vanilla "download.file" function to download a bunch of files from specified URLs. When I run the app locally, everything works great. However, after I published the Shiny app on shinyapps.io, I realized that the app tries to download files on the server, not the client computer. I know that Shiny offers the download functionality via the "downloadHandler" function, but it does not work for my web scraping script. I need the app to download a bunch of files from the web, not a dataset or a plot generated within Shiny.
If you have any ideas on how to make Shiny download files on the client computer, I would appreciate it.
Here is a simplified version of the app that I am trying to write. I am web scraping the bibliography files from a journal's website. When you click on the download button, the files will be downloaded into your home directory.
library(shiny)
library(fs)
ui <- fluidPage(
actionButton(
"download_bibs"
, "Download citations"
)
)
server <- function(input, output, session) {
home_dir <- c(
Home = fs::path_home()
)
observeEvent(input$download_bibs, {
articles <- c("aer.20200451", "aer.20190586")
issue_id <- "10.1257"
for (j in 1:length(articles)) {
i <- articles[j]
name_download <- paste0(str_remove_all(i, "\\."), ".", "bib")
path <- paste0(
home_dir
, .Platform$file.sep
, name_download
)
url_cit <- paste0(
"https://www.aeaweb.org/articles/citation-export?args%5Bformat%5D="
, "bib"
, "&args%5Bdoi%5D="
, issue_id
, "%2F"
, i
, "&args%5Btype%5D="
, "bib"
)
download.file(
url = url_cit
, destfile = path
)
}
}
)
}
shinyApp(ui = ui, server = server)
The issue is that when you run this app locally, it does what it is supposed to do. But if you publish it on the shinyapps.io, the files are downloaded on the server. The question is how to get them from the server and download on the client.
Best regards,
Alex