简体   繁体   English

R shiny - DT :: renderDataTable列宽

[英]R shiny - DT::renderDataTable column width

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 . 我生成的部分输出是使用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. 但是,如果我第二次按下actionButton,表会自行调整大小以填充输出窗口的整个宽度。

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. 我知道只使用renderTabletableOutput会解决问题,但我喜欢dataTableOutput ,并且更喜欢使用该函数。

For a MWE: 对于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) )

You could just add the width argument in the ui function instead of inside the columnDefs of the server? 你可以在ui函数中添加width参数而不是在服务器的columnDefs中添加吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM