Sebastian Zeki
Sebastian Zeki

Reputation: 6874

Showing summary stats in infoboxes in shiny

I'd love to be able to show summary stats for each column of a dataset in infoboxes in my shiny app. My app allows users to upload their own dataset with different headers for each column so I can't manually create the infoboxes- they should be created from whatever dataset has been uploaded. So far I have the code below:

library(shiny)
library(shinydashboard)
ui <- dashboardPage(
  dashboardHeader(title = "Dynamic boxes"),
  dashboardSidebar(),
  dashboardBody(
    fluidRow(
      verbatimTextOutput("summaryBasicStats"),
      uiOutput("ibox")
    )))
server <- function(input, output) {


  ############ Basic Stats Events ############


  # Return the requested dataset ----
  datasetInputBasicStats <- reactive({
    mtcars
  })

  # Generate a summary of the dataset ----
  output$summaryBasicStats <- renderPrint({
    dataset <- datasetInputBasicStats()
    summary(dataset)
  })


  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)

Upvotes: 2

Views: 960

Answers (1)

denis
denis

Reputation: 5673

Basically you need to generate the list of infoBoxes with a lapply on the summary of your table. Here is a way :

library(shiny)
library(shinydashboard)
ui <- dashboardPage(
  dashboardHeader(title = "Dynamic boxes"),
  dashboardSidebar(),
  dashboardBody(
    fluidRow(
      uiOutput("ibox")
    )))
server <- function(input, output) {


  ############ Basic Stats Events ############


  # Return the requested dataset ----
  datasetInputBasicStats <- reactive({
    mtcars
  })

  sumdiplay = reactive({
  plouf <- summary(datasetInputBasicStats())
  info <- lapply(colnames(plouf),function(coln){
    infoBox(
      coln,
      paste0(plouf[,coln],collapse = "\n"),
      icon = icon("credit-card"),
      width = 6
    )
  })
  return(info)
  })
  output$ibox <- renderUI({
    sumdiplay()
  })}
shinyApp(ui, server)

I use the list of column names in the lapply to have the name of the column in the infoBoxe:

enter image description here

Upvotes: 2

Related Questions