Search code examples
rshinyshinydashboard

Represent the selectInput value in an infoBox


The given R shiny script has a selectInput and infobox below, I just want to display the selected value in the selectInput within the infobox in the ui. Please help me with a solution and if possible, kindly avoid any scripting in the sever as I have furthur dependency. If this can be done within the UI, would be great.

## app.R ##
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
box(title = "Data", status = "primary", solidHeader = T, width = 12,
      fluidPage(
        fluidRow(
          
          column(2,offset = 0, style='padding:1px;',
                 selectInput("select the 
input","select1",unique(iris$Species)))
        ))),
  infoBox("Median Throughput Time", iris$Species)))
server <- function(input, output) { }
shinyApp(ui, server)

SelectInput


Solution

  • Trick is to make sure you know where the value of the selectInput is being assigned, which is selected_data in my example, this can be referenced within the server code by using input$selected_data.

    renderUI lets you build a dynamic element which can be rendered with uiOutput and the output id, in this case, info_box

    ## app.R ##
    library(shiny)
    library(shinydashboard)
    
    ui <- dashboardPage(
      dashboardHeader(),
      dashboardSidebar(),
      dashboardBody(
        box(title = "Data", status = "primary", solidHeader = T, width = 12,
            fluidPage(
              fluidRow(
                column(2, offset = 0, style = 'padding:1px;', 
                       selectInput(inputId = "selected_data",
                                   label = "Select input",
                                   choices = unique(iris$Species)))
                )
              )
            ),
        uiOutput("info_box")
        )
      )
    # Define server logic required to draw a histogram
    server <- function(input, output) {
       output$info_box <- renderUI({
         infoBox("Median Throughput Time", input$selected_data)
       })
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)