似乎如果盒子高度在dashboardBody中初始化为100%,它不会重新调整到页面的大小,为了解决这个问题,我们可以从Javascript手动重新调整盒子的大小。
library(shiny)
library(shinydashboard)
ui <-dashboardPage(
dashboardHeader(),
dashboardSidebar(
),
dashboardBody(
tags$head(
tags$script(
HTML("
window.onload = function() {
resize();
}
window.onresize = function() {
resize();
}
Shiny.addCustomMessageHandler ('triggerResize',function (val) {
window.dispatchEvent(new Event('resize'));
});
function resize(){
var h = window.innerHeight - $('.navbar').height() - 150; // Get dashboardBody height
$('#box').height(h);
}"
)
)
),
box( id ="box",
width = 12,
height = "100%",
solidHeader = FALSE,
status = "primary",
plotOutput("plot1",inline=F,width="100%",height="100%")
),
actionButton("plot","plot")
)
)
server <- shinyServer(function(input, output, session) {
observeEvent(input$plot,{
session$sendCustomMessage (type="triggerResize", 1)
output$plot1 <- renderPlot({plot(runif(100),runif(100))})
})
})
shinyApp(ui = ui, server = server)
或者,您可以修改 Javascript,如:
HTML("
$(document).ready(function(){
resize();
})
function resize(){
var h = window.innerHeight - $('.navbar').height() - 150; // Get dashboardBody height
$('#box').height(h);
}"
) # END html
并跳过观察者对 Javascript 的调用。
这有什么帮助吗?