Home > Software engineering >  Adjust UI element positions in shindashboardPlus header
Adjust UI element positions in shindashboardPlus header

Time:11-14

I have a Shiny application with a toggle switch on the Header that. When the toggle is pressed, it runs a long function and a message is displayed to the user that something is happening. A very simplified version is below.

The functionality that I have is fine but the UI isn't aligned. The toggle and the message displayed on the header are slightly higher than the minimizer button (hamburger) to their left. I would like to be able to move the toggle and the message down just a bit so that they are horizontally in line with the minimizer button, but I can't find a way to do this in CSS or the various shiny/shinydashboard/shinydashboardPlus functions.

Also, the output message (and htmlOutput element) is spaced far to the right of the toggle and I would like to close the gap between the toggle and the message.

enter image description here

library(shiny)
library(shinycssloaders)
library(shinydashboard)
library(shinyjs)
library(shinydashboardPlus)
library(shinyWidgets)

# Define UI for application that draws a histogram
ui <- dashboardPage(skin = 'blue', 
                    
shinydashboardPlus::dashboardHeader(title = 'Example',
    leftUi = tagList(
        useShinyjs(),
        switchInput(inputId = 'swtLabels', label = 'Labels', value = TRUE,
                    onLabel = 'Label 1', offLabel = 'Label 2',
                    onStatus = 'info', offStatus = 'info', size = 'mini', 
                    handleWidth = 230),
        htmlOutput(outputId = 'labelMessage')
        )
    ),
    dashboardSidebar(),
    dashboardBody()
)

server <- function(input, output) {  
  observeEvent(input$swtLabels, {
     shinyjs::html(id = 'labelMessage', html = 'Starting...')
     shinyjs::showElement(id = 'labelMessage')
     Sys.sleep(1)
     shinyjs::html(id = 'labelMessage', html = 'Done') 
     shinyjs::hideElement(id = 'labelMessage', anim = TRUE, animType = 'fade', time = 2.0) 
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

CodePudding user response:

Try margin-top: 6px. See below.

library(shiny)
library(shinycssloaders)
library(shinydashboard)
library(shinyjs)
library(shinydashboardPlus)
library(shinyWidgets)

# Define UI for application that draws a histogram
ui <- dashboardPage(skin = 'blue', 
                    
                    shinydashboardPlus::dashboardHeader(title = 'Example',
                                                        leftUi = tagList(
                                                          useShinyjs(),
                                                          div(switchInput(inputId = 'swtLabels', label = 'Labels', value = TRUE,
                                                                      onLabel = 'Label 1', offLabel = 'Label 2',
                                                                      onStatus = 'info', offStatus = 'info', size = 'mini', 
                                                                      handleWidth = 230), style = "margin: 6px -20px;"),
                                                          div(htmlOutput(outputId = 'labelMessage'), 
                                                              style = "margin: 8px 0px 0px -60px;")
                                                        )
                    ),
                    dashboardSidebar(),
                    dashboardBody()
)

server <- function(input, output) {  
  observeEvent(input$swtLabels, {
    shinyjs::html(id = 'labelMessage', html = 'Starting...')
    shinyjs::showElement(id = 'labelMessage')
    Sys.sleep(1)
    shinyjs::html(id = 'labelMessage', html = 'Done') 
    shinyjs::hideElement(id = 'labelMessage', anim = TRUE, animType = 'fade', time = 2.0) 
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

output

  • Related