2017-05-25 127 views
0

當切換dashboardSidebar菜單時,圖形和表格(通過R Shiny製作)不會相應地調整它們的寬度。R Shiny,dashboardSidebar在切換時中斷圖形和表格寬度

下面是一個標準圖形和表格的例子。初始化應用程序時,圖形和表格會正確填充屏幕,但切換菜單會打破這兩個元素的寬度。

這是如何解決的,因此它們的寬度始終都是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

很多幸得這個答案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) 

enter image description here

0

其實,這已被固定在shinydashboard的最新版本。只需從CRAN重新安裝它:

install.packages("shinydashboard")