提问人:StatT 提问时间:11/11/2023 最后编辑:Stéphane LaurentStatT 更新时间:11/11/2023 访问量:29
导出带有换行符的数据表
Export a datatable with line-breaks in shiny
问:
我用来截断 DataTable 中的长单词。但是,当我尝试使用按钮扩展导出表格时,导出的表格包含由“...”而不是整个单词。data.substr
有没有办法显示“......”在 Shiny 应用程序中,同时确保整个表格(没有截断)导出到 Excel?
下面是一个可重现的示例:
library(shiny)
ui <- fluidPage(
dataTableOutput("test")
)
server <- function(input, output, session) {
dt = data.frame(x=c("blablablablablablablablablablablablal","blablablablablablablablablablablablal"),y=c(1,2))
output$test <- renderDataTable({
datatable(dt, rownames = TRUE,
extensions = 'Buttons',escape=FALSE,class = 'cell-border stripe',
selection = list( mode="single"),
option=list(dom = "Bt",buttons = list("copy", list(extend = "collection", buttons = c("csv", "excel"), text = "Download") ),
columnDefs = list(list(
targets = c(0,1),
render = JS(
"function(data, type, row, meta) {",
"return type === 'display' && data != null && data.length > 35 ?",
"'<span title=\"' + data + '\">' + data.substr(0, 8) + '...</span>' : data;",
"}")
))
)
)
})
}
shinyApp(ui, server)
注意:我不想使用 downloadHandler 导出表格 :-)
感谢您的帮助
我尝试使用省略号插件,但我得到了相同的结果。
答:
1赞
Stéphane Laurent
11/11/2023
#1
您可以使用正交数据来执行此操作:
library(DT)
dt <- data.frame(
x = c(
"blablablablablablablablablablablablal",
"blablablablablablablablablablablablal"
),
y = c(1,2)
)
datatable(
dt, rownames = TRUE,
extensions = "Buttons", class = "cell-border stripe",
selection = list(mode = "single"),
options = list(
dom = "Bt",
buttons = list(
"copy",
list(
extend = "excel",
text = "Download",
exportOptions = list(
orthogonal = "export"
)
)
),
columnDefs = list(list(
targets = c(0, 1),
render = JS(
"function(data, type, row, meta) {",
" if(type === 'export') {",
" return data;",
" }",
" return type === 'display' && data != null && data.length > 35 ?",
" '<span title=\"' + data + '\">' + data.substr(0, 8) + '...</span>' : data;",
"}")
))
)
)
这不适用于 .extend = "collection"
评论
0赞
StatT
11/11/2023
非常感谢斯蒂芬,它有效!
评论