Search code examples
javascriptrshinyshinydashboard

Introduce a valueBox placeholder with shiny::req()


I would like that when my application starts up, a valueBox placeholder would appear instead of an empty space. Something like:

enter image description here

In flexdashboard the following works:

req(input$execute1)

But, in shinydashboard not (an empty space appears) See:

enter image description here

My code:

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)

Solution

  • Instead of req() you can use an if to show an empty valueBox until the user clicks the execute1 button.

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