Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for downloadButton() #39

Closed
kyle-power opened this issue May 13, 2021 · 3 comments
Closed

support for downloadButton() #39

kyle-power opened this issue May 13, 2021 · 3 comments

Comments

@kyle-power
Copy link

Workaround for PrimaryButton() as a download button

@jakubsob
Copy link
Contributor

Hello @kyle-power! A download button is not supported by shiny.fluent. See the following workaround on how you can overcome this and use any button to trigger shiny::downloadHandler:

library(shiny)
library(shiny.fluent)
library(shinyjs)

ui <- fluentPage(
  useShinyjs(),
  PrimaryButton.shinyInput(
    "downloadButton",
    text = "Download",
    iconProps = list(iconName = "Download")
  ),
  div(
    style = "visibility: hidden;",
    downloadButton("download", label = "")
  )
)

server <- function(input, output, session) {
  observeEvent(input$downloadButton, {
    click("download")
  })
  
  output$download <- downloadHandler(
    filename = function() {
      paste("data-", Sys.Date(), ".csv", sep="")
    },
    content = function(file) {
      write.csv(iris, file)
    }
  )
}

shinyApp(ui, server)

@kamilzyla
Copy link
Collaborator

Thanks @kyle-power for your question, and @jakubsob for a workaround! Starting downloads from React is a feature that we could add to shiny.react in a future release - I created issue Appsilon/shiny.react#11 to track it.

@jhk0530
Copy link

jhk0530 commented Sep 11, 2023

Hi, all.
I tried to solve my download button problem using workaround given by @jakubsob .

But in my code, download button will open new page instead of download file.

so I deleted almost every code to find what's problem, and found this.

When download button is exist in Stack it won't work properly (instead it will open new page)

So In my case, I replaced Stack to div and it worked.

Below is minimal reproducible example.

I'm even not sure what's different between div and Stack but, you may want to note.

Thanks.


library(shiny)
library(shiny.fluent)
#> 
#> Attaching package: 'shiny.fluent'
#> The following object is masked from 'package:shiny':
#> 
#>     runExample
library(shinyjs)
#> 
#> Attaching package: 'shinyjs'
#> The following object is masked from 'package:shiny.fluent':
#> 
#>     runExample
#> The following object is masked from 'package:shiny':
#> 
#>     runExample
#> The following objects are masked from 'package:methods':
#> 
#>     removeClass, show

ui <- fluentPage(
  useShinyjs(),
  PrimaryButton.shinyInput(
    "downloadButton",
    text = "Download",
    iconProps = list(iconName = "Download")
  ),
  Stack(
    style = "visibility: hidden;",
    downloadButton("download", label = "")
  )
)

server <- function(input, output, session) {
  observeEvent(input$downloadButton, {
    click("download")
  })
  
  output$download <- downloadHandler(
    filename = function() {
      paste("data-", Sys.Date(), ".csv", sep="")
    },
    content = function(file) {
      write.csv(iris, file)
    }
  )
}

shinyApp(ui, server)
#> 
#> Listening on http://127.0.0.1:3765

Created on 2023-09-12 with reprex v2.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants