繁体   English   中英

R Shiny DT响应式数据表高度

[英]R Shiny DT responsive datatable height

我正在尝试在Shiny中使用全高数据表,该表根据可用高度显示许多行,并且页面数也会发生变化。

DT responsive扩展适用于宽度。 身高是否可以相等?

答案可能是使用javascript修改show N entries顶部show N entries的框,使其具有新的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)

任何帮助将不胜感激

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

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

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM