Search code examples
rshinyshinydashboard

How to set a value in sidebarSearchForm widget


I am using sidebarSearchForm widget from shinnydashboard package.

I would like to know if it is possible set an initial value for shinnydashboard::sidebarSearchForm like it is with shinyWidgets::searchInput using its value argument:

searchInput(
    inputId = "searchText",
    label = NULL,
    placeholder = "Write your opions",
    btnSearch = icon("magnifying-glass"),
    btnReset = icon("xmark"),
    value = 'Option 1',
    width = "100%"
  )

Solution

  • We can use htmltools::tagAppendAttributes or htmltools::tagQuery to inject a value attribute to the sidebarSearchForm (consisting of a text input and a button):

    library(shiny)
    library(shinydashboard)
    
    header <- dashboardHeader()
    
    sidebar <- dashboardSidebar(
      sidebarUserPanel("User Name",
                       subtitle = a(href = "#", icon("circle", class = "text-success"), "Online"),
                       # Image file should be in www/ subdir
                       image = "userimage.png"
      ),
      tagAppendAttributes(sidebarSearchForm(label = "Enter a number", "searchText", "searchButton"), value = "my initial value", .cssSelector = "input"),
      sidebarMenu(
        # Setting id makes input$tabs give the tabName of currently-selected tab
        id = "tabs",
        menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
        menuItem("Widgets", icon = icon("th"), tabName = "widgets", badgeLabel = "new",
                 badgeColor = "green"),
        menuItem("Charts", icon = icon("bar-chart-o"),
                 menuSubItem("Sub-item 1", tabName = "subitem1"),
                 menuSubItem("Sub-item 2", tabName = "subitem2")
        )
      )
    )
    
    body <- dashboardBody(
      tabItems(
        tabItem("dashboard",
                div(p("Dashboard tab content"))
        ),
        tabItem("widgets",
                "Widgets tab content"
        ),
        tabItem("subitem1",
                "Sub-item 1 tab content"
        ),
        tabItem("subitem2",
                "Sub-item 2 tab content"
        )
      )
    )
    
    shinyApp(
      ui = dashboardPage(header, sidebar, body),
      server = function(input, output) { }
    )