根据闪亮仪表板中包含的对象调整框高度

Adjust box height based on object it includes in shiny dashboard

我有下面的 shiny 仪表板,其中 box 包含 datatable.I 希望框高度调整table 到数据高度 table(或情节)被包括在内。 box 应该比 table 每次都大一点。例如,如果我设置 pageLength=10,table 会变大,那么 box 也应该变大一点以适合。

library(shiny)
library(shinydashboard)
library(DT)

ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(),
  dashboardBody(
    tags$head(tags$script('
      // Define function to set height of "map" and "map_container"
      setHeight = function() {
        var window_height = $(window).height();
        var header_height = $(".main-header").height();

        var boxHeight = window_height - header_height - 30;

        $("#map_container").height(boxHeight);
        $("#map").height(boxHeight - 20);
      };

      // Set input$box_height when the connection is established
      $(document).on("shiny:connected", function(event) {
        setHeight();
      });

      // Refresh the box height on every window resize event    
      $(window).on("resize", function(){
        setHeight();
      });
    ')),
    # Boxes need to be put in a row (or column)
    fluidRow(
      box(id = "map_container",
          dataTableOutput("map")
      )
      
    )
  )
)

server <- function(input, output) {
  set.seed(122)
  histdata <- rnorm(500)
  
  output$map <- renderDataTable( {
    datatable(iris,options = list(pageLength=5))
  })
}

shinyApp(ui, server)

根据文档,box 的高度会根据其内容自动调整。所以如果你想让它大一点,你可以这样做:

box(
  div(
    div(style = "height: 10px;"),
    DTOutput("yourID"),
    div(style = "height: 10px;")
  )
)