R Shiny DT 响应数据表高度

R Shiny DT responsive datatable height

我正在尝试在 Shiny 中获得完整的高度数据table,它根据可用高度显示多行,页数也会发生变化。

DT responsive 扩展名用于宽度。是否可以有一个等价的高度?

一个答案可能是通过使用 javascript 的新 N 值修改顶部 show N entries 的框,并对 space 的最大值进行一些计算27=] 可以知道一行的大小。

这是一个开始:

library(shiny)

ui <- fluidPage(

    titlePanel("Arbitrary component to remove space in the page"),

    dataTableOutput('table_name')
)

server <- function(input, output) {
    output$table_name <- DT::renderDataTable({
        data.frame(a=1:100, b = 100:1, c = 101:200)
    })
}

shinyApp(ui = ui, server = server)

如有任何帮助,我们将不胜感激

下面是一个基本示例,说明如何根据 window 的高度更改 table 中的行数。这不是最有效的方法,但可以帮助您创建更好的解决方案。

请注意,调整 table 的延迟应根据您的需要进行调整。

jscode.autoHeightDT <- '
  autoHeightDT = function() {
    var offset = 100; // pixels used for other elements like title, buttons, etc

    // compute the number of rows to show in window
    var n = Math.floor(($(window).height() - offset) / $("#table_name tr").height());

    // set the new number of rows in table
    t = $("#table_name .dataTable").DataTable().page.len(n).draw();
  }

  // to adjust the height when the app starts, it will wait 0.8 seconds
  setTimeout(autoHeightDT, 800);

  // to react to changes in height of window 
  $(window).resize(function() {
    autoHeightDT();
  });

'

library(shiny)
library(DT)

ui <- fluidPage(
    tags$script(jscode.autoHeightDT), # includes JavaScript code
    titlePanel("Arbitrary component to remove space in the page"),

    dataTableOutput('table_name')
)

server <- function(input, output) {
    output$table_name <- DT::renderDataTable({
        data.frame(a=1:100, b = 100:1, c = 101:200)
    })
}

shinyApp(ui = ui, server = server)