Home > Software design >  How to retain DT Options when updating data
How to retain DT Options when updating data

Time:04-04

I have a very simple shiny app, and I want to keep autoWidth = TRUE after I hit the update button. For some reason, I get a full page-width DT after updating. What am I missing?

library(tidyverse)
library(shiny)

ui <- fluidPage(
    titlePanel("Iris button"),
    sidebarLayout(
        sidebarPanel(
          actionButton("UpdateButton", "Update!")
        ),

        # Show DT
        mainPanel(
           DTOutput("dt.iris.10")
        )
    )
)

server <- function(input, output) {
    dat.iris.10 <- iris %>%
      slice(1:10) %>%
      select(Sepal.Width)
    
    dat.react <- reactiveValues()
    dat.react$Data <- dat.iris.10
    
    output$dt.iris.10 <- renderDT(
      datatable(dat.react$Data,
                options = list(autoWidth = TRUE)
      ), server = FALSE
    )
    
    observeEvent(input$UpdateButton, {
      dat.react$Data <- dat.react$Data * 10
    }
    )
    
}

# Run the application 
shinyApp(ui = ui, server = server)

CodePudding user response:

You should use the option scrollX = TRUE.

Try this

output$dt.iris.10 <- renderDT(
    datatable(dat.react$Data 
              , options = list(autoWidth = TRUE
                               , scrollX=TRUE
                               )
    ), server = FALSE
  )
  • Related