R datatable Button with conditional formatting

一个人想着一个人 提交于 2019-12-10 19:52:03

问题


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')
      })

回答1:


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.



来源:https://stackoverflow.com/questions/37280333/r-datatable-button-with-conditional-formatting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!