r - Shiny 的仪表板不能很好地扩展

标签 r shinydashboard

第二个例子来自 http://rstudio.github.io/shinydashboard/get_started.html问题是对于某些类型的渲染,缩放效果不好。

打开的仪表板:
enter image description here

仪表板关闭:
enter image description here

仪表板关闭并打开控制台(这次它按照从一开始就应该做的那样缩放绘图)enter image description here

仪表板关闭/打开时是否可以重新渲染绘图?

最佳答案

您可以在单击仪表板打开/关闭按钮时在窗口上强制调整大小事件,方法是使用 jQuery 将函数绑定(bind)到按钮,如下所示:

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)

关于r - Shiny 的仪表板不能很好地扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36655640/

相关文章:

r - 错误 : Bioconductor version '3.13' requires R version '4.1' (R version 4. 0.2)

r - 两列facet_grid,顶部带有条形标签

html - Shiny 的情节高度在不同的桌面上表现不同

r - 如何在ggplot中动态调整aes的轴刻度限制?

r - 使用 with par(mfrow=c()) 在一张图中绘制多个 fill.contour 图

css - 在dashboardBody Shinydashboard中使用CSS更改字体系列

r - 有条件的不工作在 Shiny

css - Shiny 仪表板顶部的空白

css - 更改 Shiny 仪表板中的菜单图标

r - 如何在由变量定义的列上 ==1