daattali / shinyjs

💡 Easily improve the user experience of your Shiny apps in seconds
https://deanattali.com/shinyjs
Other
734 stars 119 forks source link

NA value for downloadHandler file name #193

Closed GeiserX closed 5 years ago

GeiserX commented 5 years ago

The minimal reproducible example can be found here: https://stackoverflow.com/questions/57056047/shiny-downloadhandler-returning-filename-na-when-enabled-disabled-by-the-shinyjs

Thanks

daattali commented 5 years ago

Can you please share the details and reprex directly in github issues?

GeiserX commented 5 years ago

The problem arises when you try to download the file, the name gets called "NA". I have another app too with the same exact problem. It's a weird behaviour in fact

If you see, the file must be named report.pdf. Instead, it's called NA

library(shiny)
library(shinyjs)

ui <- fluidPage(

  shinyjs::useShinyjs(),

  checkboxInput("checkbox","Check to enable download"),

  disabled(downloadButton("report", "Obter relatórios"))

)

server <- function(input, output) {

  output$report = downloadHandler(
    filename = "report.pdf",

    content = function(file) {
      file.copy("your dir/your pdf file.pdf", file)      
    },

    contentType = "application/pdf"

  )

  observe({

    if (!input$checkbox) {

      disable("report")

    } else {

      enable("report")

    }
  })
}

# Create Shiny app ----
shinyApp(ui, server)
daattali commented 5 years ago

Thank you. I took your sample code and tried simplifying it isolate the issue, and it does not happen anymore. In the code below, the filename works. This means that there isn't always a problem with the filename, it's something to do with this exact usecase. Can you try submitting a minimal reproducible example - that shows exactly when this issue happens? Perhaps it's because of the contentType parameter. Perhaps it's only when PDFs are involved. Perhaps it's happening because the PDF file "your dir/your pdf file.pdf" doesn't actually exist, in which case this bug isn't really a bug because the code is already broken?

library(shiny)

ui <- fluidPage(
  shinyjs::useShinyjs(), 
  checkboxInput("checkbox","Check to enable download"),
  shinyjs::disabled(downloadButton("report", "report")),
  downloadButton("report2", "report2")
)

server <- function(input, output) {
  output$report = downloadHandler(
    filename = "report.csv",
    content = function(file) {
      write.csv(cars, file) 
    }
  )
  output$report2 = downloadHandler(
    filename = "report.csv",
    content = function(file) {
      write.csv(cars, file)   
    }
  )

  observe({
    shinyjs::toggleState("report", input$checkbox)
  })
}

shinyApp(ui, server)
GeiserX commented 5 years ago

But sorry, I do not understand your example. The report downloadButton still doesn't work (for me NA appears as the filename), and the report2 does not show the bug at all, some kind of shinyjs function must be applied.

Anyway, in my project, everything exists, but the filename is still called "NA", but if you open the file, you can find all the data in there, it is just very weird.

EDIT: For you to have more info: I have latest R (3.6.1) and I am using the default driver that RStudio has to display the web.

EDIT2: It seems that if I open it with Firefox, it works. A typical "It works on my computer" kind of problem

daattali commented 5 years ago

This isn't a "works on my computer" problem, it's a "it works on a real browser, not in the RStudio browser" problem. The RStudio Viewer is not a real web browser and there are a lot of features that don't work in it. In fact up until recently, downloading files did not work at all in RStudio on Windows. For me (Windows), currently even without any shinyjs functions, the filename is never correct inside RStudio. Let me know if you reproduce any issues in a real browser

obkhan commented 5 years ago

Microsoft Edge returns NA

daattali commented 5 years ago

For what code exactly? Can you please post a minimal reproducible example and I'll investigate

obkhan commented 5 years ago

Same code you provided for first report button NA, report2 returns report.csv

seems to be with disable/enable

https://github.com/daattali/shinyjs/issues/193#issuecomment-534281708

daattali commented 5 years ago

I can't seem to reproduce. After looking into it a bit though I theorize that what's happening is that the browser is for some reason adding a download="NA" attribute to the button. Can you please check if that's correct? Have you tried updating shiny, perhaps that's why I'm not seeing the error?

obkhan commented 5 years ago

@daattali your theory is accurate about the NA attribute download on the button , shiny however is up to date

daattali commented 5 years ago

OK I'm getting closer. Looks like for some reason this download="NA" gets added when the element starts off disabled. It happens in all browser, but most browsers are smart enough to ask shiny for the filename, and some dumb browsers don't. If you remove the ="NA" from the HTML on the page, it will download the correct file. I just need to figure out why that happens (I highly doubt it's a shinyjs issue, it feels more of a shiny issue)

daattali commented 5 years ago

Found the issue. It's a bug with htmltools. https://github.com/rstudio/htmltools/issues/141