Search code examples
rvariablesprintingshinysummary

How to print the summary of a variable in R shiny?


I would like that based on a selectInput() which the client can select, the summary of the selected variable will be print in a box. My code for the ui.R is:

box(
  title = "Informed Investor", 
  status = "primary", 
  solidHeader = TRUE,
  width = 6,
  selectInput("informedDset", label="Select Category", choices = list("Informed Full" = "InformedFull", "Informed Fact" = "InformedFact", "Informed Fact Positive" = "InformedFact.Pos", "Informed Fact Negative" = "InformedFact.Neg", "Informed Emotions" = "InformedEmotions", "Informed Emotions Fact" = "InformedEmotionsFact"), selected = "Informed Full")
), 

box(
  title = "Data Table", 
  status = "warning", 
  solidHeader = TRUE,
  width = 6,
  height = 142,
  verbatimTextOutput("summaryDset")
)

And my code for server.R:

output$summaryDset <- renderPrint({
   summary(input$informedDset)
})

Solution

  • As indicated in the comments, summary returns Length Class Mode 1 character character because the input$informedDset is a character string. If you want to extract the summary of one selected variable in a dataset you can find an reproducible example below with the iris dataset :

    library(shiny)
    library(shinydashboard)
    
    ui=fluidPage(
    
     box(title = "Informed Investor", 
      status = "primary", 
      solidHeader = TRUE,
      width = 6,
      selectInput("informedDset", label="Select Category",
              choices = list("Sepal.Length"="Sepal.Length",
                             "Sepal.Width"="Sepal.Width",
                             "Petal.Length"="Petal.Length",
                             "Petal.Width"="Petal.Width",
                             "Species"="Species"), selected = "Sepal.Length")),
    
    box(
     title = "Data Table", 
     status = "warning", 
     solidHeader = TRUE,
     width = 6,
     height = 142,
     verbatimTextOutput("summaryDset")))
    
    
    server = function (input,output){
     output$summaryDset <- renderPrint({
     summary(iris[[input$informedDset]]) 
    })}
    
    shinyApp(ui, server)
    

    Is that what you want to do ?