使用 shiny::req() 引入 valueBox 占位符

Introduce a valueBox placeholder with shiny::req()

我希望当我的应用程序启动时,会出现一个 valueBox 占位符,而不是一个空的 space。类似于:

flexdashboard以下作品:

req(input$execute1)

但是,在 shinydashboard 中没有(出现空的 space)见:

我的代码:

library(shiny)
library(shinydashboard)

header <- dashboardHeader()

sidebar <- dashboardSidebar(
  sidebarMenu(

  id = "tabs", width = 300,
  
  menuItem("Analysis", tabName = "dashboard", icon = icon("list-ol"))
  
  )
)

body <- dashboardBody(

tabItems(
  
  tabItem(tabName = "dashboard", titlePanel("Analysis"), 
          
          fluidPage(
            
            column(2, 

                   box(title = "Analysis", width = 75, 
                       sliderInput(
                         inputId = 'aa', label = 'AA', 
                         value = 0.5 * 100, 
                         min = 0 * 100, 
                         max = 1 * 100, 
                         step = 1
                       ), 
                       
                       sliderInput(
                         inputId = 'bb', label = 'BB', 
                         value = 0.5 * 100, 
                         min = 0 * 100, 
                         max = 1 * 100, 
                         step = 1
                       ), 
                       
                       sliderInput(
                         inputId = 'cc', label = 'CC', 
                         value = 2.5, min = 1, max = 5, step = .15
                       ), 
                       
                       sliderInput(
                         inputId = 'dd', label = 'DD', 
                         value = 2.5, min = 1, max = 5, step = .15
                       ), 
                       
                       actionButton("execute1", "Click me")
                   )
            ), 
            
            column(8, 
                   tagAppendAttributes(class = "b1", valueBoxOutput(outputId = "box1", width = 6)))
             )
          )
       )
    )

ui <- dashboardPage(header, sidebar, body)

server <- function(input, output, session) {
  
  ac <- function(aa, bb, cc, dd) {
    (aa + cc) + (bb + dd)
  }
  
  reac_1 <- reactive({
    tibble(
      aa = input$aa, 
      bb = input$bb, 
      cc = input$cc, 
      dd = input$dd
    )
  })
  
  pred_1 <- reactive({
    temp <- reac_1()
    ac(
      aa = input$aa, 
      bb = input$bb, 
      cc = input$cc, 
      dd = input$dd
    )
  })
  
  output$box1 <- renderValueBox({
    req(input$execute1)
    expr = isolate(valueBox(
      value = pred_1(), 
      subtitle = "cap"
    ))
  })
  
}

shinyApp(ui, server)

您可以使用 if 代替 req() 来显示空的 valueBox,直到用户单击 execute1 按钮。

  output$box1 <- renderValueBox({ 
    if (input$execute1 == 0) {
      valueBox("?", subtitle = "cap")
    } else {
      expr = isolate(valueBox(
        value = pred_1(), 
        subtitle = "cap"
      ))
    }
  })