【问题标题】:How to overcome width parameter in shinyDashboard valueBox being ignored when wrapped in renderUI and htmlOutput当包装在renderUI和htmlOutput中时如何克服shinyDashboard valueBox中的宽度参数被忽略
【发布时间】:2022-11-04 00:28:30
【问题描述】:

我想创建 shinyDashboard valueBoxes。这些数量取决于选择的输入,所以我按照this example 进行渲染n使用 renderUI、htmlOutput 和 tagList 的 valueBoxes。但是,当我这样做时,valueBox 中的宽度参数似乎被忽略了,并且 valueBoxes 占据了页面的剩余宽度并垂直堆叠。如果在 UI 中手动呈现,它们的宽度是正确的,并且位于单个流畅的行上。

这是一个例子。这里 valueBoxes 的数量被硬编码为 3,但实际上这将由动态数据确定:

library(shiny)
library(shinyWidgets)
library(shinydashboard)

# Define UI for application that draws a histogram
ui <- fluidPage(
  useShinydashboard(),
  
  # Application title
  titlePanel(title = 'Title'),
  
  sidebarLayout(
    sidebarPanel(width = "3",
                 selectInput(inputId = 'pick', 'Pick a letter', 
                             choices = c('A','B','C'))
    ),
    
    mainPanel(
      uiOutput("stats")
    )
  )
)

server <- function(input, output, session) {
  #how many statistics to show - in reality this will be dynamic
  nstats <- 3
  stats <- c(-10,0,10)  
  
  #dynamically create the right number of htmlOutputs 
  #https://stackoverflow.com/questions/31686773/shiny-dynamic-number-of-output-elements-plots
  output$stats <- renderUI({
    vboxes <- lapply(1:nstats, function(i) {
      outputId <- paste0("valuebox_", i)
      htmlOutput(outputId)
    })
    tagList(vboxes)
  })
  #now populate these with valueboxes containing the ith statistic
  for(i in 1:nstats){
    local({
      #my_i <- i
      outputId <- paste0("valuebox_", i)
      temp <- stats[i]
      output[[outputId]] <- renderValueBox({
        valueBox(
          subtitle = 'Change measure',
          width = 2,
          value = paste0(temp,"%"),
          icon = icon('chart-line')
        )
      })
    })
  }
}


# Run the application 
shinyApp(ui = ui, server = server)

这将生成以下内容,其中 valueBoxes 占据了所有 9 列,而不是每列 2 列。

我在 htmlOutput 中尝试过 inline=TRUE 但这没有什么区别。如何使 width=2 受到尊重,或者我需要将它移到哪里?谢谢

【问题讨论】:

    标签: r shiny shinydashboard


    【解决方案1】:

    将htmlOutput 更改为valueBoxOutput:

      output$stats <- renderUI({
        vboxes <- lapply(1:nstats, function(i) {
          outputId <- paste0("valuebox_", i)
          valueBoxOutput(outputId)
        })
        tagList(vboxes)
      })
    

    顺便说一句,我认为这是使用modules 会带来好处的经典示例。这意味着您不需要跟踪服务器或 UI 中的 valueBox ID,并且可以将 value box 中数据的所有操作委托给模块。这将使您的主要 ui 和服务器功能更短、更清晰。

    【讨论】:

      猜你喜欢
      • 2016-12-11
      • 2016-05-27
      • 1970-01-01
      • 1970-01-01
      • 2015-08-28
      • 1970-01-01
      • 2020-02-23
      • 1970-01-01
      • 2014-09-28
      相关资源
      最近更新 更多