Datatable options broke after data update

6 views
Skip to first unread message

Sam

unread,
May 16, 2018, 3:58:00 AM5/16/18
to Shiny - Web Framework for R
Hi ,
I have a reactive dataframe which is displayed in a DataTable with some options (especially the width of columns).
The initialization of the DT is ok but when the dataframe is updated (in the example, by clicking on the action button), the options seem not to be taken in account (the width of columns is maximum).

How can I keep the options ?

Here is a reproducible example


#########

library(shiny) 
library(DT) 
shinyApp( 
    ui = fluidPage(
        sidebarPanel(
            actionButton("foo", "Foo")),
        # display table
        mainPanel(DT::dataTableOutput('x1'),
                  tags$script(HTML("Shiny.addCustomMessageHandler('unbind-DT', function(id) {
                                   Shiny.unbindAll($('#'+id).find('table').DataTable().table().node());
                                   })")),
                  verbatimTextOutput("out"))), 
    
    server = function(input, output, session) { 
        
        rv <- reactiveValues(
            n = 1,
            df = data.frame(col=seq(1:3))
        )
        
        observeEvent(input$foo,{
            rv$df <- rbind(rv$df, nrow(rv$df)+1)
        })
        
       
        # reactive dataset
        data <- reactive({
            req(rv$df)
            session$sendCustomMessage('unbind-DT', 'x1')
            rv$df
        })
        
        
        output$x1 <- DT::renderDataTable({
            data()
        },server=FALSE,escape=FALSE,selection='none',
        options=list(preDrawCallback=JS(
            'function() {
            Shiny.unbindAll(this.api().table().node());}'),
            drawCallback= JS(
                'function(settings) {
                Shiny.bindAll(this.api().table().node());}'),
            dom = 't',
            autoWidth=TRUE,
            rownames = FALSE,
            columnDefs = list(list(width='50px',targets= "_all"))
        ))
        
}
    ) 


Best
Reply all
Reply to author
Forward
0 new messages