Search code examples
rshiny

Display multiple infoboxes within one reactive function


I wish to know if it is possible to create multiple infoBoxes with only one reactive function "ibox" as in the script below. I shall pass the values for all the infoboxes below within the ibox reactive function and see all of them getting displayed together.

library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "Dynamic boxes"),
dashboardSidebar(),
dashboardBody(
  fluidRow(
  infoBoxOutput("ibox")
  )))
server <- function(input, output) {
output$ibox <- renderInfoBox({
  infoBox(
    "Title",
    5,
    icon = icon("credit-card")
  )
  infoBox(
    "Title",
    5,
    icon = icon("credit-card")
  )
  infoBox(
    "Title",
    4,
    icon = icon("credit-card")
  )

  })}
  shinyApp(ui, server)

Solution

  • As mentioned in the comments, you could use renderUI and uiOutput. However, note that renderUI only actually renders the last statement in its body. In order to render multiple objects, we can place them in a list (or column, fluidRow, etc.). Working example:

    library(shiny)
    library(shinydashboard)
    ui <- dashboardPage(
      dashboardHeader(title = "Dynamic boxes"),
      dashboardSidebar(),
      dashboardBody(
        fluidRow(
          uiOutput("ibox")
        )))
    server <- function(input, output) {
      output$ibox <- renderUI({
        list(
          infoBox(
            "Title",
            5,
            icon = icon("credit-card")
          ),
          infoBox(
            "Title",
            5,
            icon = icon("credit-card")
          ),
          infoBox(
            "Title",
            4,
            icon = icon("credit-card")
          )
        )
      })}
    shinyApp(ui, server)