r - Shiny :情节的动态高度调整

标签 r shiny shinydashboard

问题:在下面的 Shiny 应用程序中,用户可以根据选择的输入添加值框中显示的信息。如果用户选择所有可能的选项,则 UI 将如屏幕截图所示。

问题: 绘图(与值框位于同一行)是否有可能调整高度(因此绘图的底部与最后一个值框的底部对齐)?

enter image description here

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(),
  
  dashboardSidebar(
    selectizeInput(
      inputId = "select",
      label = "Select country:",
      choices = c("CH", "JP", "GER", "AT", "CA", "HK"),
      multiple = TRUE)
  ),
  
  dashboardBody(
    fluidRow(column(2, uiOutput("ui1")),
             column(10, plotOutput("some_plot"))))#,
                # column(4, uiOutput("ui2")),
                # column(4, uiOutput("ui3")))
)

server <- function(input, output) {
  
  output$ui1 <- renderUI({
    req(input$select)
    
    lapply(seq_along(input$select), function(i) {
      fluidRow(
        valueBox(value = input$select[i],
                 subtitle = "Box 1",
                 width = 12)
      )
    })
  })
  
  output$some_plot <- renderPlot(
    plot(iris)
  )
}

shinyApp(ui = ui, server = server)

最佳答案

您可以在 renderPlot 中调整高度。我已将最小值设置为 3 值框高度。因此,在添加 3 个值框后它开始增加高度。您可以根据需要修改它。试试下面的代码。

  library(shiny)
  library(shinydashboard)
  
  ui <- dashboardPage(
    dashboardHeader(),
    
    dashboardSidebar(
      selectizeInput(
        inputId = "select",
        label = "Select country:",
        choices = c("CH", "JP", "GER", "AT", "CA", "HK"),
        multiple = TRUE)
    ),
    
    dashboardBody(
      fluidRow(column(2, uiOutput("ui1")),
               column(10, plotOutput("some_plot"))))#,
    
    # column(4, uiOutput("ui2")),
    # column(4, uiOutput("ui3")))
  )
  
  server <- function(input, output) {
    plotht <- reactiveVal(360)
    observe({
      req(input$select)
      nvbox <- length(input$select)
      if (nvbox > 3) {
        plotheight <- 360 + (nvbox-3)*120
      }else plotheight <- 360
      plotht(plotheight)
    })
    
    output$ui1 <- renderUI({
      req(input$select)
      
      lapply(seq_along(input$select), function(i) {
        fluidRow(
          valueBox(value = input$select[i],
                   subtitle = "Box 1",
                   width = 12)
        )
      })
    })
    
    observe({
      output$some_plot <- renderPlot({
        plot(iris)
      }, height=plotht())
    })
 
    
  }
  
  shinyApp(ui = ui, server = server)

关于r - Shiny :情节的动态高度调整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63634653/

相关文章:

r - R中的遗传算法

r - 在直方图中添加图例(格子)

r - 将 CSS 加载到 Shiny 服务器时出现延迟

css - 在 selectInput 旁边放置按钮

r - 将数字列更改为因子并向数据分配标签/级别

重新编码因子水平

R Shiny : Unable to dispatch task for application, 有 1 个任务正在进行中

r - 是否可以将图的笔刷区域 "clear"变亮?

javascript - 将多个项目粘贴到 selectizeInput [ Shiny ]

r - 与 Shiny 的仪表板情节集成