闪亮的仪表板不能很好地扩展

Shiny dashboard does not scale well

我从 http://rstudio.github.io/shinydashboard/get_started.html 中获取了第二个示例,问题是对于某些类型的渲染,缩放比例不是很好。

仪表板已打开:

仪表板已关闭:

仪表板关闭并打开控制台(这次它缩放绘图,因为它应该从一开始就完成)

仪表板为 closed/opened 时是否可以重新渲染绘图?

当单击仪表板 open/close 按钮时,您可以在 window 上强制执行调整大小事件,方法是使用 jQuery 将函数绑定到按钮,如下所示:

library(shinydashboard)

ui <- dashboardPage(

  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(),
  dashboardBody(
    tags$script('
      // Bind function to the toggle sidebar button
      $(".sidebar-toggle").on("click",function(){
        $(window).trigger("resize"); // Trigger resize event
      })'
    ),

    # Boxes need to be put in a row (or column)
    fluidRow(
      box(plotOutput("plot1", height = 250)),

      box(
        title = "Controls",
        sliderInput("slider", "Number of observations:", 1, 100, 50)
      )
    )
  )
)

server <- function(input, output, session) {
  set.seed(122)
  histdata <- rnorm(500)

  output$plot1 <- renderPlot({
    data <- histdata[seq_len(input$slider)]
    hist(data)
  })
}

shinyApp(ui, server)

如果您不想在所有元素上强制执行调整大小事件,您可以在每次切换边栏时使用 shiny::uiOutput 和 shiny::renderUI 函数重新创建 plotOutput。

library(shinydashboard)

ui <- dashboardPage(

  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(),
  dashboardBody(
    tags$script('
      // Bind function to the toggle sidebar button
      $(".sidebar-toggle").on("click",function(){
        // Send value to Shiny 
        Shiny.onInputChange("toggleClicked", Math.random() );
      })'
    ),

    # Boxes need to be put in a row (or column)
    fluidRow(
      #box(plotOutput("plot1", height = 250)),
      box(uiOutput('plotUi')),

      box(
        title = "Controls",
        sliderInput("slider", "Number of observations:", 1, 100, 50)
      )
    )
  )
)

server <- function(input, output, session) {
  # Helper function to create the needed ui elements
  updateUI <- function(){
    output$plotUi <- renderUI({
      plotOutput("plot1", height = 250)
    })
  }

  # Plot data to plotOutput
  updatePlot <- function(){
    output$plot1 <- renderPlot({
      hist( data() )
    })
  }

  set.seed(122)
  histdata <- rnorm(500)

  # Initialize UI and create plotOutput
  updateUI()
  updatePlot()

  # Create a reactive dataset
  data <- eventReactive(input$slider,{
    histdata[seq_len(input$slider)]
  })

  # This is triggered when the toggle dashbord button is clicked
  # this is achived by the javascript binding in the ui part
  observeEvent(input$toggleClicked,{
    updateUI()
    updatePlot()
  })
}

shinyApp(ui, server)