在我的
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")})
})
}
)
我所期望的是,无论情节有多长,文本行始终保留在页面底部。我做错了什么?那我该怎么办呢?
感谢@Jan,我想出了一个反应性渲染页面的解决方案:
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()})
}
)