5

I'm making an app in shiny, and have run into a small but irritating problem. Part of the output that I produce is outputted using DT::renderDataTable. There are only two columns, and the width of the first column will depend on the input dataset, so I don't want to absolutely fix the width to anything. I've seen this thread, and the code in one of the answers works to some extent. However, if I push the actionButton a second time, the table resizes itself to fill the entire width of the output window.

Is there a way to prevent the table from resizing itself after pressing the action button again? I know that using just renderTable and tableOutput will solve the problem, but I like the dataTableOutput, and would prefer to use that function.

For a MWE:

library("shiny")
library("DT")

mwe_ui <- shinyUI(fluidPage(
    sidebarLayout(
    sidebarPanel(
      titlePanel("Example"),
      sliderInput(inputId = "df",
                  label = "Degrees of Freedom:",
                  min=1 , max=50 , value=1 , step=1
      ),
      actionButton(inputId = "compute1",
                   label = "Sample"
      )
    ),
    mainPanel(
      dataTableOutput( outputId = "summary" )
    )
  )))

mwe_server <- function(input, output) {

  temp01  <- reactive({
    compute1 <- input$compute1
    if( compute1 > 0 ){
      isolate({
        aa <- round( runif(6, 4,20 ) )
        bb <- character()
        for( ii in 1:6 ){
          bb[ii] <- paste0(sample(letters, size=aa[ii]), collapse="")
        }
        xx <- matrix( round(rt(6, df=input$df), 4), nrow=6, ncol=1 )
        return( data.frame(xx) )
      })
    }
  }) 

  ##############

  output$summary <-  DT::renderDataTable({
    temp02 <- temp01()
  }, rownames=FALSE,
  options = list(autoWidth = TRUE, 
                    columnDefs = list(list(width = "125px", targets = "_all"))
                    )
  )

}


runApp( list(ui=mwe_ui, server=mwe_server) )
Community
  • 1
  • 1
Jelsema
  • 151
  • 1
  • 5
  • I see that you're pre-fix the width to `125px`, why not fix that for the table? – Pork Chop Oct 06 '15 at 13:22
  • From my understanding, this width of `125px` was not an absolute fixed width, just a "preferred" width. In the actual app I was making, the I used those exact specifications for column width, and the first column was able to be much wider than the second column. I've edited the example to have some character strings of varying length to illustrate what I mean. Does specifying a table width define an absolute width, or will it expand to the contents of the table? – Jelsema Oct 06 '15 at 15:34
  • I can't believe that this doesn't have a straight solution, like manualcolumnresize in Rhandsontable – Diego Sep 20 '21 at 17:45

1 Answers1

0

You could just add the width argument in the ui function instead of inside the columnDefs of the server?

library("shiny")
library("DT")

mwe_ui <- shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
      titlePanel("Example"),
      sliderInput(inputId = "df",
                  label = "Degrees of Freedom:",
                  min=1 , max=50 , value=1 , step=1
      ),
      actionButton(inputId = "compute1",
                   label = "Sample"
      )
    ),
    mainPanel(
      dataTableOutput( outputId = "summary" , width="125px")
    )
  )))

mwe_server <- function(input, output) {

  temp01  <- reactive({
    compute1 <- input$compute1
    if( compute1 > 0 ){
      isolate({
        aa <- round( runif(6, 4,20 ) )
        bb <- character()
        for( ii in 1:6 ){
          bb[ii] <- paste0(sample(letters, size=aa[ii]), collapse="")
        }
        xx <- matrix( round(rt(6, df=input$df), 4), nrow=6, ncol=1 )
        return( data.frame(xx) )
      })
    }
  }) 

  ##############
  output$summary <-  DT::renderDataTable({
    temp02 <- temp01()
  }, rownames=FALSE,
  options = list(autoWidth = TRUE)
  ) 
}

runApp( list(ui=mwe_ui, server=mwe_server) )
SeGa
  • 9,454
  • 3
  • 31
  • 70