Krisselack
Krisselack

Reputation: 511

R datatable Button with conditional formatting

I want to create a shiny app performing a database query. The (simple) idea is to create a table, apply conditional formatting on the columns and allow the reported table to save to excel. I tried different solutions, but the usage of a datatable download-button seemed the most convenient.

After extensive search I managed to apply a download button (updated dt-package from github) and to apply a specific formatting (formatStyle) in the browser display of the shiny-app. This will allow me in future to apply conditional formatting for the unique columns. However, when using the save-button this formatting gets lost in the saved file (See code fragment below).

In my understanding, the reason for this is that the formatStyle function is only applied after the datatable-call. Would there be a way to apply the formatStyle function earlier? The reason for creating the resulttab-object is, that the formatStyle-function is applied on specific columns selected by their names, while the tabelle-function is generated in the shiny reactive-environment.

output$tab <- DT::renderDataTable({
   resulttab <- tabelle() 
    datatable(resulttab, extensions = 'Buttons', options = list(
      dom = 'Bfrtip',
      buttons = 
        list('copy', 'print', list(
          extend = 'collection',
          buttons = list(list(extend='csv',
                              filename = 'blBericht'),
                         list(extend='excel',
                              filename = 'blBericht'),
                         list(extend='pdf',
                              filename= 'blBericht')),
          text = 'Download')))) %>%  formatStyle('Tribrommethan',  color = 'red', backgroundColor = 'orange', fontWeight = 'bold')
      })

Upvotes: 4

Views: 1054

Answers (1)

Krisselack
Krisselack

Reputation: 511

The Export-Button of datatables with the option excel is apparently a wrapped csv-export. Thus formatting cannot be exported. My workaround is now using the shiny-Export-Button and the XLConnect package.

I couldn't find a solution to display the workbook (created via XLConnect in the downloadHandler) in the browser. Thus I will use a renderDataTable function without formatting for Browser-display, as defining the (numerous) formatting conditions twice in different packages is not viable.

Upvotes: 1

Related Questions