【问题标题】:how can I eliminate placeholder white space from shiny apps?如何从闪亮的应用程序中消除占位符空白?
【发布时间】:2019-11-12 08:30:03
【问题描述】:

在制作闪亮的应用程序时,我经常为如何在页面上为用户安排图表和文本输出而苦恼。所以,我想尝试让用户可以选择要显示的输出。例如,用户可能能够显示 3 个图形和 2 个文本块,但可能只想显示第一个图形和一个文本块。我通常可以使用渲染函数中的 if 语句来完成此操作。但是,我发现即使......用户没有指示应该显示情节,闪亮仍然会添加空白。这似乎是某种占位符空间,最终会在应用程序中留下大漏洞。在我的示例中,应用程序显示了第二个绘图,但是有一大块空白使页面看起来很丑。

我发现这个:Shiny: unwanted space added by plotOutput() and/or renderPlot() whihc 不太适用。似乎没有解决此特定问题的帖子。

library(shiny)

ui <- fluidPage(

   titlePanel("Old Faithful Geyser Data"),

   sidebarLayout(
      sidebarPanel(
         sliderInput("bins",
                     "Number of bins:",
                     min = 1,
                     max = 50,
                     value = 30)),
      mainPanel(
         plotOutput("distPlot"),
         plotOutput("distPlot2"))))


server <- function(input, output) {

   output$distPlot <- renderPlot({
      if(input$bins == 4){
      x    <- faithful[, 2] 
      bins <- seq(min(x), max(x), length.out = input$bins + 1)
      hist(x, breaks = bins, col = 'darkgray', border = 'white')
   }})

   output$distPlot2 <- renderPlot({
      x    <- faithful[, 2] 
      bins <- seq(min(x), max(x), length.out = input$bins + 1)
      hist(x, breaks = bins, col = 'darkgray', border = 'white')})
   }

shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r ggplot2 shiny


    【解决方案1】:

    在这种特殊情况下,有两个可能的选择是将第一个图包裹在 shiny::conditionalPanel() 中或使用 shiny::renderUI()

    shiny::conditionalPanel() 示例

    library(shiny)
    
    ui <- fluidPage(
    
      titlePanel("Old Faithful Geyser Data"),
    
      sidebarLayout(
        sidebarPanel(
          sliderInput("bins",
                      "Number of bins:",
                      min = 1,
                      max = 50,
                      value = 30)),
        mainPanel(
          conditionalPanel("input.bins == 4", plotOutput("distPlot")),
          plotOutput("distPlot2"))))
    
    server <- function(input, output) {
    
      output$distPlot <- renderPlot({
          x    <- faithful[, 2] 
          bins <- seq(min(x), max(x), length.out = input$bins + 1)
          hist(x, breaks = bins, col = 'darkgray', border = 'white')
        })
    
      output$distPlot2 <- renderPlot({
        x    <- faithful[, 2] 
        bins <- seq(min(x), max(x), length.out = input$bins + 1)
        hist(x, breaks = bins, col = 'darkgray', border = 'white')})
    }
    
    shinyApp(ui = ui, server = server)
    

    shiny::renderUI() 示例

    library(shiny)
    
    ui <- fluidPage(
    
      titlePanel("Old Faithful Geyser Data"),
    
      sidebarLayout(
        sidebarPanel(
          sliderInput("bins",
                      "Number of bins:",
                      min = 1,
                      max = 50,
                      value = 30)),
        mainPanel(
          uiOutput("distPlots"))))
    
    
    server <- function(input, output) {
      output$distPlot <- renderPlot({
          x    <- faithful[, 2] 
          bins <- seq(min(x), max(x), length.out = input$bins + 1)
          hist(x, breaks = bins, col = 'darkgray', border = 'white')
        })
    
      output$distPlot2 <- renderPlot({
        x    <- faithful[, 2] 
        bins <- seq(min(x), max(x), length.out = input$bins + 1)
        hist(x, breaks = bins, col = 'darkgray', border = 'white')})
    
      output$distPlots <- renderUI({
        if(input$bins == 4)
          fluidPage(plotOutput("distPlot"), plotOutput("distPlot2"))
        else
          plotOutput("distPlot2")
      })
    }
    
    shinyApp(ui = ui, server = server)
    

    这两种方法都会产生这样的结果:

    【讨论】:

    • 这是一个很好的解决方案,但我可能有太多的输出组合依赖于不同的输入组合,因此不实用。通过在有问题的图表上调整 plotOutput 函数中的 height 参数,我能够接近我所需要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 2022-01-19
    • 2020-06-18
    • 1970-01-01
    • 2017-07-27
    • 2016-02-09
    • 1970-01-01
    相关资源
    最近更新 更多