[英]shiny renderPlot with dynamic height
问题我想动态更改渲染图的高度,以便如果它包含大量数据点,则会为该图分配更多空间。 然后应该简单地移动图下方的内容。
可以使用renderPlot
的height
参数,但是绘图会溢出到下一个元素中,我想避免这种情况。
我可以通过使用uiOutput
来规避它,但我想知道是否可以在不依赖renderUI
情况下获得相同的行为?
预期结果
我希望在绘图大小发生变化时移动绘图下方的div
,而不使用renderUI
截图
library(shiny)
library(ggplot2)
ui <- fluidPage(
fluidRow(
column(width = 2, sliderInput("n", "Number of Rows:", 1, 49, 10)),
column(width = 10, plotOutput("plot"))
),
fluidRow( ## the plot oevrflows into this div when the plot grows
column(width = 12, div(style = "background:red; height: 100px"))
),
fluidRow(
column(width = 10, offset = 2, uiOutput("ui"))
),
fluidRow( ## this div is properly moved down as the plot grows
column(width = 12, div(style = "background:red; height: 100px"))
)
)
server <- function(input, output, session) {
data <- reactive(data.frame(id = factor(1:input$n, 1:input$n),
y = rpois(input$n, 200)))
output$plot <- renderPlot(
qplot(id, y, geom = "col", fill = y, data = data()) +
coord_flip(), height = function() 20 * NROW(data())
)
output$ui <- renderUI(plotOutput("plot2", height = 20 * NROW(data())))
output$plot2 <- renderPlot(
qplot(id, y, geom = "col", fill = y, data = data()) +
coord_flip()
)
}
您可以在 ui 部分编写plotOutput("plot", height = "auto")
。 plotOutput 的默认高度固定为 400 像素。 通过设置 height="auto" 情节自动调整到内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.