3
votes

I'm putting the finishing touches on a shinydashboard. The dashboard uses googleAuthR for authentication via google oauth. Everything is working... but I currently have to put the login button in either the dashboardSidebar or dashboardBody, and I'd really really like it up where the dropdowns go in dashboardHeader. Unfortunately, it seems like shinydashboard's header is picky about what can go up in the header. Is there a hack (or less than a hack) to put stuff up there?

Here's a thing that definitely doesn't work, for example:

ui = dashboardPage(
  dashboardHeader(
    title = "My Awesome Dashboard"
    , p('Pretend this is a login button')
  )
  , dashboardSidebar(
    p('I don't want the login here.')
  )
  , dashboardBody(
    p('I don't want the login here either.')
  )
)

server = function(input, output, session) {
}

shinyApp(
  ui = ui
  , server = server
)
2

2 Answers

16
votes

You can place anything in the header, but it needs to be a li tag of class dropdown. See the following example:

ui = dashboardPage(
  dashboardHeader(
    title = "My Awesome Dashboard",
    tags$li(class = "dropdown",
            tags$li(class = "dropdown", textOutput("logged_user"), style = "padding-top: 15px; padding-bottom: 15px; color: #fff;"),
            tags$li(class = "dropdown", actionLink("login", textOutput("logintext"))))
  )
  , dashboardSidebar(), dashboardBody())

server = function(input, output, session) {
  logged_in <- reactiveVal(FALSE)

  # switch value of logged_in variable to TRUE after login succeeded
  observeEvent(input$login, {
    logged_in(ifelse(logged_in(), FALSE, TRUE))
  })

  # show "Login" or "Logout" depending on whether logged out or in
  output$logintext <- renderText({
    if(logged_in()) return("Logout here.")
    return("Login here")
  })

  # show text of logged in user
  output$logged_user <- renderText({
    if(logged_in()) return("User 1 is logged in.")
    return("")
  })

}

shinyApp(ui = ui, server = server)

enter image description here

0
votes

Ok, I think I figured it out based on Adding a company Logo to ShinyDashboard header , but I'm not sure yet why it works. Here's the general idea:

my_header = dashboardHeader(
  title = "My More Awesome Dashboard"
)

my_header$children[[3]]$children[[3]] = p('Login goes here', style = 'float: right')

ui = dashboardPage(db_header, dashboardSidebar, dashboardBody)

Now to see if I can find documentation somewhere explaining the $children stuff...