How to Get The R Shiny Downloadhandler Filename to Work

downloadButton/ downloadHandler does not recognize filename argument

I had the same issue when I used the RStudio preview window and was able to solve this issue by always opening a browser with the command

runApp(launch.browser = TRUE)

Shiny DownloadHandler doesn't update filename using Sys.time()

This is because you don't give a function but a value as argument for filename. That's the reason you always have the same filename: a value is assigned when the downloadHandler is initiated, whereas a function is evaluated at every click on the downloadButton.

So wrap your code for the filename in a function, and your problem is solved:

library(shiny)

if (interactive()) {

ui <- fluidPage(
downloadButton("downloadData", "Download")
)

server <- function(input, output) {
# Our dataset
data <- mtcars

output$downloadData <- downloadHandler(
filename = function(){
paste("example",gsub(":","-",Sys.time()), ".csv", sep="")
},
content = function(file) {
write.csv(mtcars,file)
}
)
}

runApp(list(ui=ui,server=server),launch.browser=T)
}

This information can also be found on the following article of RStudio:

https://shiny.rstudio.com/articles/download.html

Downloading file with input for filename in Shiny app

Make a call to input$name at the beginning of your observe. This should do it, althought append=TRUE wont work, as it will always create a new file, rather than append to an existing csv-file.

Here is the new server.R code:

server <- function(input, output, session) {
observe({
input$name
data <- data.frame(x=1:50, y=rnorm(50))

serverDownloadCSV <- function(name, data) {
downloadHandler(
filename=function() {
if (name == "") {
paste("Untitled", "csv", sep=".")
} else {
paste(name, "csv", sep=".")
}
},
content = function(file) {
write.table(data, file, row.names=FALSE, append=TRUE, sep=",")
}
)
}

output$save <- serverDownloadCSV(name=input$name, data=data)
})
}

How to fix 'File not found' when using the downloadHandler?

The download handler does not know that the pseudo_df data frame was created. You probably want to have one reactive that makes the data frame and then separate render and download handlers that call the reactive that creates the data frame. So for example

make_df <- reactive({})  # code that makes the data frame goes here
output$results <- renderTable({make_df()})
output$downloadData <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(make_df(), file) # notice the call to the reactive again
}
)

R Shiny: downloadHandler - problem in content argument

Try this

  myplot <- reactive({
dat <- get_data()
lik <- likert(dat %>% ungroup() %>% select(Antwortkategorie) %>%
as.data.frame(),
grouping = if (input$group) dat %>% pull(grp))
plot(lik)
})

output$plot <- renderPlot({
myplot()
})

output$downplot <- downloadHandler(
filename = function() {
paste(input$question, ".png", sep = "")
},
content = function(file) {
png(file)
print(myplot())
dev.off()
},
contentType = "image/png"
)


Related Topics



Leave a reply



Submit