title: How to download a formatted Excel into a selected directory
body: <p>I would like my Shiny app to apply conditional formatting to the result and let my users save the formatted table to a folder of their choosing.</p>
  
  <p>Here is my code. I got this message when I ran it.</p>
  
  <pre><code>Warning: Error in as.data.frame.default: cannot coerce class "structure("Workbook", package = "openxlsx")" to a data.frame
  </code></pre>
  
  <p><strong>app.R:</strong>   </p>
  
  <pre><code> ui &lt;- fluidPage(
    column(DT::dataTableOutput('sw'), width = 6),
    br(),
  
    downloadButton('download', "Save")
  )
  
  
  server &lt;- function(input, output, session) {
  
    #Dummy data. Actual data is reactive.
    result &lt;- reactive({
      a &lt;- c (NA, 120, 91, 74)
      b &lt;- c(120, NA, 109, 119)
      c &lt;- c(91, 109, NA, 121)
      d &lt;- c(74, 119, 121, NA)
  
      return(data.frame(a, b, c,d))
    })
  
    output$sw &lt;- DT::renderDataTable({
        brks    &lt;- quantile(result(), probs = seq(.05, .95, .05), na.rm = TRUE)
        colfunc &lt;- colorRampPalette(c("indianred1","white","mediumseagreen"))
        clrs    &lt;- colfunc(length(brks) + 1)
        DT::datatable(result(), rownames = TRUE, options = list(paging = FALSE, searching = FALSE))  %&gt;% formatStyle(colnames(result()), backgroundColor = styleInterval(brks, clrs))
    })
  
    output$download &lt;- downloadHandler(
      filename = function() {paste("Result.xlsx")},
      content  = function(file) {
  
        wb &lt;- createWorkbook()
        negStyle &lt;- createStyle(bgFill = "#FFC7CE")
        posStyle &lt;- createStyle(bgFill = "#C6EFCE")
  
        addWorksheet(wb, "Sheet1")
        writeData(wb = wb, sheet="Sheet1", x = result(), startRow = 1, startCol = 1)
  
        posStyle &lt;- createStyle(bgFill = "#C6EFCE")
        conditionalFormatting(wb, "Sheet1", cols=3:ncol(result()), rows=2:(1+nrow(result())), rule="&lt;=80", style = negStyle)
        conditionalFormatting(wb, "Sheet1", cols=3:ncol(result()), rows=2:(1+nrow(result())), rule="&gt;120", style = posStyle)
  
        write.xlsx(x = wb, file)
      }
    )
  }
  
  shinyApp(ui = ui, server = server)
  </code></pre>
  
  <p>UPDATE: 2/6/2018</p>
  
  <p>Changing the following</p>
  
  <pre><code>write.xlsx(x = wb, file)
  </code></pre>
  
  <p>to</p>
  
  <pre><code>saveWorkbook(wb, file)
  </code></pre>
  
  <p>works perfectly.</p>