在Shiny中渲染数据表

时间:2018-05-09 12:36:33

标签: r shiny

我有这个表包含一些带有大文本的字段,比如摘要,当我渲染它们时,它看起来很糟糕。

有没有办法只显示部分文字,使用" ..."?

像:

id;name;author;abstract
123;Lucca;Adam;"Abstract text..."

这是一张如下图片的图片:

数据表:

Data Table

1 个答案:

答案 0 :(得分:0)

正如评论中提到的,基于Shiny renderDataTable | how to limit text size displayed,你可以实现它:

# generating new variable in iris dataset with 5000 characters per row
iris$text <- apply(iris, 1, function(x) {paste0(letters[sample(26,5000,T)],collapse = "")})
# increasing the volume of data to 15K rows
iris <- do.call("rbind", replicate(100, iris, simplify = FALSE))

# Shiny app example
library(shiny)
library(DT)

shinyApp(
  ui = fluidPage(
    fluidRow(
      column(12,
             dataTableOutput('table')
      )
    )
  ),
  server = function(input, output) {
    output$table <- renderDataTable(iris,
                                    options = list(columnDefs = list(list(
                                      targets = 6,
                                      render = JS(
                                        "function(data, type, row, meta) {",
                                        "return type === 'display' && data.length > 6 ?",
                                        "'<span title=\"' + data + '\">' + data.substr(0, 6) + '...</span>' : data;",
                                        "}")
                                    ))), callback = JS('table.page(3).draw(false);'))
  }
)