如何使用 adjustable 列实现 R shiny table?
How to implement a R shiny table with adjustable columns?
我正在寻找一种方法来使 table 列能够处理 data/text 不同的未知大小。
table 的预期行为基本上类似于 Excel cells/colums,即
- 默认不换行
- 对于长文本,只显示适合指定列宽的内容(在下面的示例中,第 1 行第 2 列显示“非常”)
- 可以通过拖动显示更多或更少文本的边框来调整列宽
下面的例子可以处理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"))
))
}
)
2.) 可以通过呈现列来实现,即 trim 此处建议的数据 How to Truncate text in DataTable in R Shiny?
但是 3.) 似乎很棘手,因为我找不到使用 DT 甚至可以拖动列边框的提示。即使是,似乎也不能直接与 2.) 兼容。在更改列边界后不重新渲染 table。
有什么想法或建议如何处理吗?
尝试使用 rhandsontable 而不是 Datatable。它有一些惊人的功能。其中之一是手动调整列大小。
https://handsontable.com/docs/8.3.0/demo-resizing.html
本文档将帮助您解决问题。
我不知道如何使 2 和 3 轻松一致,但您可以“手动”调整(通过滑块输入?)要显示的字符数和 column.s
以你为例,我为第一列添加了带滑块输入的文本截断(默认 10 个字符)+ 居中 + 手动宽度调整(另一个滑块输入)
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),
"}"
)
)
}
)
)
)
}
)
我正在寻找一种方法来使 table 列能够处理 data/text 不同的未知大小。
table 的预期行为基本上类似于 Excel cells/colums,即
- 默认不换行
- 对于长文本,只显示适合指定列宽的内容(在下面的示例中,第 1 行第 2 列显示“非常”)
- 可以通过拖动显示更多或更少文本的边框来调整列宽
下面的例子可以处理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"))
))
}
)
2.) 可以通过呈现列来实现,即 trim 此处建议的数据 How to Truncate text in DataTable in R Shiny?
但是 3.) 似乎很棘手,因为我找不到使用 DT 甚至可以拖动列边框的提示。即使是,似乎也不能直接与 2.) 兼容。在更改列边界后不重新渲染 table。
有什么想法或建议如何处理吗?
尝试使用 rhandsontable 而不是 Datatable。它有一些惊人的功能。其中之一是手动调整列大小。 https://handsontable.com/docs/8.3.0/demo-resizing.html 本文档将帮助您解决问题。
我不知道如何使 2 和 3 轻松一致,但您可以“手动”调整(通过滑块输入?)要显示的字符数和 column.s
以你为例,我为第一列添加了带滑块输入的文本截断(默认 10 个字符)+ 居中 + 手动宽度调整(另一个滑块输入)
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),
"}"
)
)
}
)
)
)
}
)