title: R/Shiny - How to write a .csv reactively including a actionButton?
body: <p>I am trying to figure out how to write a .csv based on selections made by the end user. The selections made will subset the "geodata.csv" and write a separate "solution.csv" file in the application folder.</p>
  
  <p><strong>N.B.</strong> - I have created a <a href="https://github.com/ucg8j/ShinyWriteCsv" rel="nofollow">github repo</a> to make solving the question easier. It contains the geodata.csv, ui.R &amp; server.R but no solution.csv yet! </p>
  
  <p><strong>geodata.csv</strong></p>
  
  <pre><code>Postcode,HC,BSL,Position Location
  10,1,A,C
  10,1,A,D
  10,1,A,D
  11,1,B,C
  11,1,B,C
  </code></pre>
  
  <p><strong>ui.R</strong></p>
  
  <pre><code>shinyUI(
   pageWithSidebar(
    headerPanel('Min. working example - write a csv based on user input'),
  
    sidebarPanel(
     selectInput("filter1", "First selection:" 
                , choices = c(Choose='', "A", "B")
                #, multiple=T
                ),
  
     selectInput("filter2", "Second selection:", 
                choices = c(Choose='', "C", "D")
                ),
     br(),
     p("Include actionButton to prevent write occuring before user finalises selection"),  
     actionButton("generateButton","Write Data")    
    ),
    mainPanel()
   )
  )
  </code></pre>
  
  <p><strong>server.R</strong></p>
  
  <pre><code># Load data
  setwd("/Users/lukesingham/SOtestApp")
  geodata &lt;- read.csv("geodata.csv", na.string = "#N/A", row.names=NULL)
  
  # Reactivity to subset data ####
  shinyServer(function(input, output) {
    geodatasetInput &lt;- reactive({
  
      # BSL switch
      selection &lt;-switch(input$BSL
                         , A = "A"
                         , B = "B"
                         ) 
      # Location switch    
      selection2 &lt;-switch(input$Location
                         , C = "C"
                         , D = "D"
                         )
  
      # subset based on selection
      Subgeodata &lt;- subset(geodata, BSL == selection &amp; Position.Location == selection2)
  
      # Execute selections on data upon button-press
      input$generateButton
  
      # aggregate by postcode
      Subgeodata &lt;- Subgeodata[1:2] #no longer need other columns
      AggSubGdata &lt;- aggregate(. ~ Postcode, data=Subgeodata, FUN=sum)
      isolate(write.csv(AggSubGdata
                , file = "/Users/lukesingham/SOtestApp/solution.csv"
                , row.names=F
      ))
    })
  })
  </code></pre>
  
  <p><strong>solution.csv</strong></p>
  
  <p>For example, based on user selections of <code>A</code> and <code>D</code> the solution file should look like this:</p>
  
  <pre><code>Postcode,HC
  10,2
  </code></pre>