plotOutput / uiOutput 生成超出闪亮

问题描述 投票:0回答:1

在我的

shiny
应用程序中,我希望一行文本始终保留在页面底部。因此,我使用了两个
fluidRow()
来构建 UI,上行显示页面,下行显示文本。但是,我生成的绘图过高,并且与底部文本重叠。

这是重现错误的示例代码。

library(tidyverse)
library(shiny)

shinyApp(
  ui = fluidPage(
    fluidRow(
      navbarPage(
        "tabs",
        tabPanel(
          "Widgets",
          fluidRow(column(12, actionButton("draw", "Draw!"), style = "height:88vh;"))
        ),
        tabPanel(
          "Plots",
          fluidRow(column(12, uiOutput("Plot"), style = "height:88vh;"))
          # I used the `uiOutput()` because in my actual case I want to show multiple plots in a navlistPanel() with many tabs.
          # both `plotOutput()` and `uiOutput()` will trigger the error.
        )
      )
    ),
    fluidRow(column(12, p("here's the bottom text", style = "text-align:center;")))
  ),
  server = function(input, output) {
    observeEvent(input$draw, {
      output$p1 <- renderPlot({
        ggplot(mtcars, aes(x = mpg, y = disp)) + geom_point()
      }, height = 1200)
      
      output$Plot <- renderUI({plotOutput("p1")})
      
    })
  }
)

我所期望的是,无论情节有多长,文本行始终保留在页面底部。我做错了什么?那我该怎么办呢?

css r shiny
1个回答
0
投票

感谢@Jan,我想出了一个反应性渲染页面的解决方案:

  • 在shiny应用程序首次启动时,上行显示一个高度为88%的
    div
  • 一旦触发事件按钮,上排将渲染一张无限高度的图片。
shinyApp(
  ui = fluidPage(
    fluidRow(
      navbarPage(
        "tabs",
        tabPanel(
          "Widgets",
          fluidRow(column(12, actionButton("draw", "Draw!"), style = "height:88vh;"))
        ),
        tabPanel(
          "Plots",
          fluidRow(column(12, uiOutput("Plot")))
        )
      )
    ),
    fluidRow(
      column(12, p("here's the bottom text", style = "text-align:center;"))
    )
  ),
  server = function(input, output) {
    init.p <- reactiveVal(0)
    
    observeEvent(input$draw, {init.p(init.p() + 1)})
    
    plotpage <- reactive({
      init <- init.p()
      
      if (init == 0) {
        return(tagList(div(style = "height:88vh;width:100%")))
      } else {
        return(
          renderPlot({
            ggplot(mtcars, aes(x = mpg, y = disp)) + geom_point()
          }, height = 1200)
        )
      }
    })

    output$Plot <- renderUI({plotpage()})
  }
)
© www.soinside.com 2019 - 2024. All rights reserved.