0

So basically I am trying to make a small setup of sorts and once a certain analysis is done, I would want to export a certain dataset generated to a predefined location and a predefined name (based on the inputs selected earlier). For this purpose, I used the action button which when clicked does this,

observeEvent(input$export_button, {
        write.csv(input_dummy_data4ads,paste0("Dummy Files/",unique(input_dummy_data4ads$Dependent_Variable),"_", unique(input_dummy_data4ads$Model_Type),"_", unique(input_dummy_data4ads$AGM),".csv"),row.names = F,na="")
           }) 

The issue here is that if I click the action button once, it generates the desired csv file and at the desired location too. But after pressing it once, it takes the value of 1 (input$export_button) so when I select a new set of inputs using the radio buttons and generate a new plot based on that (by clicking another action button), the app saves a new csv file with a new name (based on the new inputs) at the desired location. What I am trying to do is to reset the value of the action button so that the new csv file is created only when I click it every time.

I tried to understand this but could not incorporate it https://github.com/rstudio/shiny/issues/167

pogibas
  • 27,303
  • 19
  • 84
  • 117
Dan Schmidt
  • 117
  • 1
  • 2
  • 10

1 Answers1

0

There are specific functions in shiny for this, use downloadButton in your ui and downloadHandler in server.

server.R:

output$export_data <- downloadHandler(
  filename = function() {
    paste0("Dummy Files/", unique(input_dummy_data4ads$Dependent_Variable), "_", unique(input_dummy_data4ads$Model_Type), "_", unique(input_dummy_data4ads$AGM), ".csv")
  },
  content = function(con) {
    write.csv(input_dummy_data4ads, con, row.names = F, na = "")
  }
)

ui.R:

downloadButton("export_data", "Export")
Kevin Arseneau
  • 6,186
  • 1
  • 21
  • 40
  • downloadButton doesn't seem to autosave a file I am generating in the server in a pre defined location and a pre defined name. That is why I tried using actionButton which worked (or so I thought) till I realised that it is just working efficiently for the first time. – Dan Schmidt Oct 06 '17 at 07:03
  • @DanSchmidt, based on your comment, I assume you are doing this only on your local machine and not using *shiny-server*, normally these files are transient. It seems you are looking more to reset the `actionButton`, which is just to `isolate` your code, see [this](https://github.com/rstudio/shiny/issues/167) – Kevin Arseneau Oct 06 '17 at 07:12
  • Sir thank you for replying and well I saw the same link but like I mentioned I was not able to incorporate it. – Dan Schmidt Oct 06 '17 at 10:00
  • @DanSchmidt, if you put up some reproducible code we can see if we can't solve your problem – Kevin Arseneau Oct 06 '17 at 11:04