更改“ plotOutput”中绘图区域的高度根据数字的数量(自动)闪亮

发布于 2025-01-31 21:41:59 字数 640 浏览 2 评论 0原文

我在Shiny的服务器部分中具有“ RenderPlot”功能,该功能产生了1到4个数字。 如何根据数字数量(自动)更改闪亮应用程序中的绘图区域的高度? 我的意思是4个数字的高度应大于2个数字的高度。 我在UI部分中使用了Wight =“ Auto”,Height =“ Auto”,但我有错误。因此,我为其分配了一个固定尺寸(高度=“ 800”),但是如果我有1个数字,那将是很大的。如果我有3或4个,它们会变小。 请查看我的代码的一部分:

#UI section
plotOutput("fancyPlot",  inline = F, height = "800")

#Server section
 output$fancyPlot <- renderPlot({
#I get the plot_list_final which has some plots (1 or 2 or 3 or 4).
              n <-  lenght(plot_list_final )
              nCol <- floor(sqrt(n))
              p_last =  do.call("grid.arrange", c(plot_list_final, ncol=nCol))
              return(p_last) 
})

I have a " renderplot " function in the server section of Shiny that produces 1 to 4 figures.
how can I change the height of the plot area in the shiny app based on the number of the figures (automatically)?
I mean the height for 4 figures should be bigger than the height for 2 figures.
I used wight = "auto" , height = "auto" in the UI section but I got error. So I assign a fixed size to it (height = "800"), but if I have 1 figure it would be very big. if I have 3 or 4 they get smaller.
please see a part of my code:

#UI section
plotOutput("fancyPlot",  inline = F, height = "800")

#Server section
 output$fancyPlot <- renderPlot({
#I get the plot_list_final which has some plots (1 or 2 or 3 or 4).
              n <-  lenght(plot_list_final )
              nCol <- floor(sqrt(n))
              p_last =  do.call("grid.arrange", c(plot_list_final, ncol=nCol))
              return(p_last) 
})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幽蝶幻影 2025-02-07 21:41:59

您可以在grid.arrange中单独完成每个图,例如fluidPage stliplaylayout。在这种情况下,每个图都有单独的数字。在这种情况下,用renderui生成UI,具有对行数和列的调整功能。

这是解决方案:

n <- 4 #(1 or 2 or 3 or 4)
nCol <- floor(sqrt(n))
nRow <- ceiling(n/nCol)
height <- "800"

ui <- fluidPage(
      uiOutput("plotgrid")
)

server <- function(input, output, session) {
  output$plotgrid <- renderUI({
    lapply(1:nRow, function(k) {
      do.call(splitLayout,
              c(lapply(1:nCol,
                       function(l) {
                         plotOutput(paste("plot", k, l, sep = "-"), height = height)
                       })
              )
      )
    })
  })
  
  observe({
    lapply(1:nRow, function(k) {
      lapply(1:nCol,
             function(l) {
               if (k*l <= n) {
                 output[[paste("plot", k, l, sep = "-")]] <- renderPlot({
                   # this is your plot
                   plot(rnorm(10), rnorm(10))
                 })
               }
             })
    })
  })
  
}

shinyApp(ui = ui, server = server)

You can do every plot in your grid.arrange individually, for example inside fluidPage with splitLayout. In this case, you have separate figure for each plot. UI is generated with renderUI in this case to have capability of adjustment for number of rows and columns.

Here is the solution:

n <- 4 #(1 or 2 or 3 or 4)
nCol <- floor(sqrt(n))
nRow <- ceiling(n/nCol)
height <- "800"

ui <- fluidPage(
      uiOutput("plotgrid")
)

server <- function(input, output, session) {
  output$plotgrid <- renderUI({
    lapply(1:nRow, function(k) {
      do.call(splitLayout,
              c(lapply(1:nCol,
                       function(l) {
                         plotOutput(paste("plot", k, l, sep = "-"), height = height)
                       })
              )
      )
    })
  })
  
  observe({
    lapply(1:nRow, function(k) {
      lapply(1:nCol,
             function(l) {
               if (k*l <= n) {
                 output[[paste("plot", k, l, sep = "-")]] <- renderPlot({
                   # this is your plot
                   plot(rnorm(10), rnorm(10))
                 })
               }
             })
    })
  })
  
}

shinyApp(ui = ui, server = server)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文