1

我试图在 Shiny 中有一个完整的高度数据表,它根据可用高度显示多行,并且页数也会发生变化。

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

show N entries一个答案可能是使用 javascript通过一个新值修改顶部的框,N并在知道行大小的情况下对表可以占用的最大空间进行一些计算。

这是一个开始:

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)

任何帮助将不胜感激

4

1 回答 1

3

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

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

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)
于 2019-08-13T23:16:27.583 回答