如何从闪亮的应用程序中消除占位符空白? [英] how can I eliminate placeholder white space from shiny apps?

查看:35
本文介绍了如何从闪亮的应用程序中消除占位符空白?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我发现了这一点:

When making shiny apps I often struggle with how to arrange plots and text outputs on the page for the user. So, I like to try an make it possible for a user to select which output to display. For instance, the user may be able to display 3 graphs and 2 chunks of text but may only want to display the first graph and one chunk of text. I can usually accomplish this using if statements inside of render functions. But, I have found shiny will still add white space even if...the user does not indicate the plot should be displayed. This appears to be some sort of placeholder space that ends up leaving big holes in the app. In my example, the app displays the 2nd plot, but there is a big piece of white space that makes the page look ugly.

I have found this: Shiny: unwanted space added by plotOutput() and/or renderPlot() whihc doesnot quite apply. There doesn't seem to be a post that addresses this specific problem.

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)

解决方案

In this particular case, two possible options are to either wrap the first plot in a shiny::conditionalPanel() or make use of shiny::renderUI().

shiny::conditionalPanel() Example

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() Example

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)

Both approaches produce this:

这篇关于如何从闪亮的应用程序中消除占位符空白?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆