R shiny - DT::renderDataTable column width

二次信任 提交于 2020-01-11 09:06:11

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/32955610/r-shiny-dtrenderdatatable-column-width

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