简体   繁体   English

R Shiny DT 渲染 shinyinputs 打破了宽度定义

[英]R Shiny DT rendering shinyinputs breaks the width definition

I am trying to render different shinyinputs (in the example below I have checkboxes, but I am also rendering dropdowns) in a datatable on DT with R Shiny, using the shinyInput function below.我正在尝试使用下面的 shinyInput 函数在带有 R Shiny 的 DT 上的数据表中渲染不同的闪亮输入(在下面的示例中,我有复选框,但我也在渲染下拉列表)。

It works great, I was able to render all the components that I wanted inside the cells.它工作得很好,我能够在单元格内渲染我想要的所有组件。

Unfortunately now I am trying to make the whole table readable and I am facing this issue.不幸的是,现在我正试图让整个表格可读,我正面临这个问题。

Without the checkboxes the table is rendered properly and the column width are taken from the coldef, where I have a list of lists containing targets and widths.如果没有复选框,表格将正确呈现,列宽取自coldef,其中我有一个包含目标和宽度的列表列表。

As soon as I include checkboxes or any other shiny component, the columndef is not working anymore, not only for the columns containing checkboxes but for ALL of the columns, it just seems that the columndef is not present.一旦我包含复选框或任何其他闪亮的组件,columndef 就不再工作了,不仅对于包含复选框的列而且对于所有列,似乎 columndef 不存在。

I trying solving my way around and I am not sure if this is a bug or if there even is any workaround for this issue.我试图解决我的问题,但我不确定这是否是一个错误,或者是否有任何解决此问题的方法。 I spent so much time on this table that I would feel quite bad dropping it just because it's looking so bad with the checkboxes column rendered with 300px width.我在这张桌子上花了这么多时间,我会因为它看起来很糟糕而放弃它而感觉很糟糕,因为复选框列以 300 像素的宽度呈现。

In the example below you can keep or drop the variable newvar from the dataframe to see the behaviour changing on the inclusion of checkboxes, even though the first 3 columns aren't changing.在下面的示例中,您可以保留或删除数据框中的变量 newvar 以查看包含复选框时的行为变化,即使前 3 列没有变化。

library(DT)

ui <- basicPage(
  h2("The mtcars data"),
  DT::dataTableOutput("mytable")
)

server <- function(input, output) {
  
  shinyInput <- function(FUN, len, id, ...) {
    inputs <- character(len)
    for (i in seq_len(len)) {
      inputs[i] <- as.character(FUN(paste0(id, i), ...))
    }
    inputs
  }
  
  mtcarsx <- data.frame(mtcars, newvar=paste0(shinyInput(checkboxInput,nrow(mtcars),"mychbx",label="",value=FALSE,width=NULL)))

  colDef <- list(
    list(
      targets=0,
      width="150px"
    ),
    list(
      targets=1,
      width="300px"
    ),
    list(
      targets=2,
      width="500px"
    )
  )

  output$mytable = DT::renderDataTable({
    DT::datatable(mtcarsx, 
                  escape = FALSE, 
                  selection = 'none', 
                  rownames = FALSE, 
                  options = list(searching = FALSE, 
                                 ordering  = FALSE,
                                 columnDefs = colDef,
                                 autoWidth = FALSE
                  ))
  })
  
}

shinyApp(ui, server)

I used the information from @K-Rhode from this answer: https://stackoverflow.com/a/49513444/4375992我从这个答案中使用了来自@K-Rhode 的信息: https ://stackoverflow.com/a/49513444/4375992

From what I can tell, your primary issue is that the column width of the checkbox is too wide, yes?据我所知,您的主要问题是复选框的列宽太宽,是吗? Well this should do it.好吧,这应该这样做。 Add a classname to the columnDefs for the checkbox column, then in css adjust the width of that class为复选框列的 columnDefs 添加一个类名,然后在 css 中调整该类的宽度

library(DT)
library(shiny)

ui <- basicPage(
  h2("The mtcars data"),
  DT::dataTableOutput("mytable"),
  tags$head( #CSS added to shrink the column with
    tags$style('td.small .shiny-input-container{width:auto;}
                td.small{width:30px;}
                ')
  )
)

server <- function(input, output) {
  
  shinyInput <- function(FUN, len, id, ...) {
    inputs <- character(len)
    for (i in seq_len(len)) {
      inputs[i] <- as.character(FUN(paste0(id, i), ...))
    }
    inputs
  }
  
  mtcarsx <- data.frame(mtcars, newvar=paste0(shinyInput(checkboxInput,nrow(mtcars),"mychbx",label=NULL,value=FALSE,width=NULL)))
  
  colDef <- list(
    list(
      targets=0,
      width="150px"
    ),
    list(
      targets=1,
      width="300px"
    ),
    list(
      targets=2,
      width="500px"
    ),
    list(
      targets = 11,
      className = "small" #Class name added so we can adjust the width of the checkbox element above in CSS
    )
  )
  
  output$mytable = DT::renderDataTable({
    DT::datatable(mtcarsx, 
                  escape = FALSE, 
                  selection = 'none', 
                  rownames = FALSE, 
                  options = list(searching = FALSE, 
                                 ordering  = FALSE,
                                 columnDefs = colDef,
                                 autoWidth = FALSE
                  ))
  })
  
}

shinyApp(ui, server)

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

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