ShinyDashboard仪表板中的登录按钮标题仪表板、按钮、标题、ShinyDashboard

2023-09-04 02:20:52 作者:浅瞳

我正在给仪表板做最后的润色。仪表板使用googleAuthR通过Google OAuth进行身份验证。一切都很正常。但目前我必须将登录按钮放在仪表板边栏或仪表板Body中,我真的非常希望它位于仪表板Header中的下拉列表所在的位置。不幸的是,shinydashboard的标题似乎对标题中的内容很挑剔。有没有黑客(或不是黑客)把东西放在上面?

这里有一件事情肯定不起作用,例如:

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
)

推荐答案

基于shinydashboard搭建你的仪表板 一

您可以在Header中放置任何内容,但它必须是dropdown类的li标记。请参见以下示例:

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)