rstudio / shiny

Easy interactive web applications with R
https://shiny.posit.co/
Other
5.35k stars 1.87k forks source link

downloadHandler gives confusing error message if type of filename() is wrong #2615

Open hadley opened 5 years ago

hadley commented 5 years ago
library(shiny)
ui <- fluidPage(
  downloadButton("download")
)
server <- function(input, output, session) {
  output$download <- downloadHandler(
    filename = function() NULL,
    content = function(path) write.csv(mtcars, path)
  )
}
shinyApp(ui, server)

After clicking on button I see this in the console:

Warning in rep(yes, length.out = len) :
  'x' is NULL so the result will be NULL
Warning: Error in <-: replacement has length zero
  [No stack trace available]

I'd suggest throwing an error if filename doesn't return a character vector of length 1

MFathyM commented 5 years ago

The error disappears if you assign the file name:

library(shiny) ui <- fluidPage( downloadButton("download") ) server <- function(input, output, session) { output$download <- downloadHandler( filename = function() "my_file", content = function(path) write.csv(mtcars, path) ) } shinyApp(ui, server)

nathancfox commented 2 years ago

I concur, just spent an hour trying to diagnose the same issue.