简体   繁体   English

如何实现具有可调节列的 R shiny 表?

[英]How to implement a R shiny table with adjustable columns?

I am looking for a way to have table columns that can handle data/text of different unknown size.我正在寻找一种方法来拥有可以处理不同未知大小的数据/文本的表格列。

The desired behavior of the table is basically like Excel cells/colums, ie表的期望行为基本上就像 Excel 单元格/列,即

  1. No wrapping by default默认不换行
  2. For long texts only show as much as fits within the specified column width (in the example below in row 1 column 2 show "A very ")对于长文本,仅在指定的列宽范围内显示(在下面的示例中,第 1 行第 2 列显示“非常”)
  3. Column width can be adjusted by dragging the borders showing more or less of the text可以通过拖动显示更多或更少文本的边框来调整列宽

The following example can handle 1.)下面的例子可以处理 1.)

library(shiny)
library(DT)

shinyApp(
  ui <- fluidPage(
    #tags$head(tags$style("#table  {white-space: nowrap;  }")),
    DT::dataTableOutput("table"),
 
  ),
  
  server <- function(input, output) {
    
    output$table <- DT::renderDataTable({
      data.frame(a=c(1,2,3),
                 b=c("A very long text!!!!!!!","B","C"), 
                 c=c("A","B","C"))
    }, class="hover cell-border stripe nowrap",
    options = list(
      autoWidth = TRUE,
      columnDefs = list(list(width = '50px', targets = "_all"))
    ))
    
  }
)

The 2.) could be achived by rendering the column, ie trim the data as suggested here How to Truncate text in DataTable in R Shiny? 2.) 可以通过渲染列来实现,即按照此处的建议修剪数据如何在 R Shiny 中截断数据表中的文本?

However 3.) seems to be tricky since I couldn't find a hint that dragging the column boarder is even possible with DT.但是 3.) 似乎很棘手,因为我找不到使用 DT 甚至可以拖动列边界的提示。 Even if it was, seems to be not directly compatible with 2.) without rerendering the table after the column boarders are changed.即使是这样,似乎也不能直接与 2.) 兼容,而无需在更改列边界后重新渲染表格。

Any ideas or advise how to handle this?有什么想法或建议如何处理吗?

Try using rhandsontable instead of Datatable.尝试使用 rhandsontable 而不是 Datatable。 It has some amazing features.它有一些惊人的功能。 One of which is the manual column resizing.其中之一是手动调整列大小。 https://handsontable.com/docs/8.3.0/demo-resizing.html This documentation will help you with your problem. https://handsontable.com/docs/8.3.0/demo-resizing.html本文档将帮助您解决问题。

I don't know a way to make 2 and 3 easily consistent but you can "manually" adjust (via slider input?) the number of characters to show & the width of the column.s我不知道如何使 2 和 3 轻松一致,但您可以“手动”调整(通过 slider 输入?)要显示的字符数和 column.s 的宽度

Using you example I added text truncation with slider input (default 10 chars) + centering + manual width ajustment (another slider input) for the 1st col.以您为例,我为第一列添加了带有 slider 输入(默认 10 个字符)+居中 + 手动宽度调整(另一个 slider 输入)的文本截断。

library(shiny)
library(DT)


shinyApp(
  ui <- fluidPage(
    #tags$head(tags$style("#table  {white-space: nowrap;  }")),
    tagList(
      sliderInput("nb_chars","Number of chars to truncate to: ",min = 5,max = 80,step = 5,value = 10),
      sliderInput("col1_width","Width of col 1 in px :",min = 5,max = 100,step = 10,value = 50,post = "px")
    ),
    DT::dataTableOutput("table"),
    
  ),
  
  server <- function(input, output) {
    df = data.frame(
                    a=c(1,2,3),
                    b=c("A very long text!!!!!!!","B","C"), 
                    c=c("A","B","C"))
    nb_cols = ncol(df)+1#row names is 1 additional column
    col_widths <- reactive(c(input$col1_width,rep(50,nb_cols-1)))
    
    output$table <- DT::renderDataTable({
      df
    }, class="hover cell-border stripe nowrap",
    options = list(
      columnDefs = lapply(1:length(col_widths), function(i){
        width = col_widths()[i]
        list(
          width = paste0(width,"px"),
          targets = i-1
          ,className = 'dt-center'
          ,render =
            JS(
              "function(data, type, row, meta) {",
              sprintf("return type === 'display' && data && data.length > %s ?",input$nb_chars),
              sprintf("'<span title=\"' + data + '\">' + data.substr(0, %s) + '...</span>' : data;",input$nb_chars),
              "}"
            )
        )
      }

        )
    )
    )
    
  }
)

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

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