当切换仪表板侧栏菜单时,图形和表格(通过R Shiny制作)不会相应地调整它们的宽度。
以下是标准图表和表格的示例。初始化应用程序时,图形和表格正确填充屏幕,但切换菜单会破坏两个元素的宽度。
如何修复它,使它们的宽度始终为100%?
library(shiny)
library(shinydashboard)
library(dygraphs)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
sidebarMenu(id = "menu_tabs",
menuItem("Test", tabName = "page_1", icon = icon("table"), selected = TRUE)
)
),
dashboardBody(
tabItems(
tabItem(tabName = "page_1",
fluidRow(
column(width = 12, offset = 0,
box(width = 12,
dygraphOutput("dy_plot", height = "310px")
)
)
),
fluidRow(
column(width = 12, offset = 0,
box(width = 12,
dataTableOutput('mytable')
)
)
)
)
)
)
)
server <- function(input, output) {
output$mytable = renderDataTable({
mtcars
},
options = list(
lengthMenu = c(30),
pageLength = 30,
searching = FALSE,
paging = FALSE,
ordering = FALSE,
scrollX = TRUE))
output$dy_plot <- renderDygraph({
lungDeaths <- cbind(mdeaths, fdeaths)
dygraph(lungDeaths)
})
}
shinyApp(ui, server)
答案 0 :(得分:0)
很多信用可归到这个答案Shiny dashboard does not scale well,您可以强制调整大小
rm(list = ls())
library(shiny)
library(shinydashboard)
library(dygraphs)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
sidebarMenu(id = "menu_tabs",menuItem("Test", tabName = "page_1", icon = icon("table"), selected = TRUE)
)
),
dashboardBody(
tags$script('
// Bind function to the toggle sidebar button
$(".sidebar-toggle").on("click",function(){
$(window).trigger("resize"); // Trigger resize event
})'
),
tabItems(
tabItem(tabName = "page_1",
fluidRow(
column(width = 12, offset = 0,
box(width = 12,
dygraphOutput("dy_plot", width = "100%", height = "310px")
)
)
),
fluidRow(
column(width = 12, offset = 0,
box(width = 12,
dataTableOutput('mytable')
)
)
)
)
)
)
)
server <- function(input, output) {
output$mytable = renderDataTable({
mtcars
},
options = list(
lengthMenu = c(30),
pageLength = 30,
searching = FALSE,
paging = FALSE,
ordering = FALSE,
scrollX = TRUE))
output$dy_plot <- renderDygraph({
lungDeaths <- cbind(mdeaths, fdeaths)
dygraph(lungDeaths)
})
}
shinyApp(ui, server)
答案 1 :(得分:0)
实际上,这已在shinydashboard的最新版本中修复。只需从CRAN重新安装它:
install.packages("shinydashboard")