Search code examples
rshinyshinydashboard

Align widget inside shinydashboard box?


Is there a way of aligning a widget inside a shinydashboard box? For example, in the following app:

library(shiny)
library(shinydashboard)
library(shinyWidgets)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(box(
    title = "Test", width = 4, solidHeader = TRUE, status = "primary",
    dropdownButton(
      inputId = "mydropdown",
      label = "Controls",
      icon = icon("sliders"),
      status = "primary",
      circle = FALSE,
      numericInput("obs", "Observations:", 10, min = 1, max = 100)
    ),
    plotOutput('plot')
  ))
)

server <- function(input, output) {
  output$plot <- renderPlot({
    hist(runif(input$obs))
  })
}

shinyApp(ui, server)

enter image description here

I would like to align the dropdownButton widget to the bottom right corner of the Test box. How can I do that?


Solution

  • Just put the dropdownButton after the plot and inside a div with a class "pull-right"

    library(shiny)
    library(shinydashboard)
    library(shinyWidgets)
    
    ui <- dashboardPage(
      dashboardHeader(),
      dashboardSidebar(),
      dashboardBody(box(
        title = "Test", width = 4, solidHeader = TRUE, status = "primary",
        plotOutput('plot'),
        div(class = "pull-right",
          dropdownButton(
            inputId = "mydropdown",
            label = "Controls",
            icon = icon("sliders"),
            status = "primary",
            circle = FALSE,
            numericInput("obs", "Observations:", 10, min = 1, max = 100)
          )
        )
      ))
    )
    
    server <- function(input, output) {
      output$plot <- renderPlot({
        hist(runif(input$obs))
      })
    }
    
    shinyApp(ui, server)