闪亮仪表板中的动态车身

dynamic body in shiny dashboard

我正在使用 R/shinydasboard 创建一个我放在登录屏幕后面的网络应用程序。

我在根据边栏菜单选项卡渲染主体时遇到问题。

我已尝试确保其中一个选项卡项 selected = TRUE 仍然无济于事。

示例代码如下:

require(shiny)
require(shinydashboard)

Logged <- FALSE;
LoginPass <- 0; #0: not attempted, -1: failed, 1: passed

login <- box(title = "Login",textInput("userName", "Username (user)"),
             passwordInput("passwd", "Password (test)"),
             br(),actionButton("Login", "Log in"))

loginfail <- box(title = "Login",textInput("userName", "Username"),
                 passwordInput("passwd", "Password"),
                 p("Username or password incorrect"),
                 br(),actionButton("Login", "Log in"))

mainbody <- div(tabItems(
  tabItem(tabName = "t_item1", box(title = "Item 1 information")),
  tabItem(tabName = "t_item2", box(title = "Item 2 information")),
  tabItem(tabName = "t_item3", box(title = "Item 3 information"))
)
)

header <- dashboardHeader(title = "dashboard header")

sidebar <- dashboardSidebar(uiOutput("sidebarpanel"))

body <- dashboardBody(uiOutput("body"))

ui <- dashboardPage(header, sidebar, body)

server <- function(input, output, session) {
  USER <<- reactiveValues(Logged = Logged, LoginPass = LoginPass)
  observe({
    if (USER$Logged == FALSE) {
      if (!is.null(input$Login)) {
        if (input$Login > 0) {
          username <- isolate(input$userName)
          password <- isolate(input$passwd)
          #Id.username <- which(my_username == Username)
          if (username == "user" & password == "test") {
            USER$Logged <<- TRUE
            USER$LoginPass <<- 1
          }
          USER$LoginPass <<- -1
        }
      }
    }
  })

  output$sidebarpanel <- renderUI({
    if (USER$Logged == TRUE) {
      div(
        sidebarMenu(
          menuItem("Item 1", tabName = "t_item1", icon = icon("line-chart"), selected = TRUE),
          menuItem("Item 2", tabName = "t_item2", icon = icon("users")),
          menuItem("item 3", tabName = "t_item3", icon = icon("dollar"))
        )
      )}
  })
  output$body <- renderUI({
    if (USER$Logged == TRUE) {
      mainbody
    }
    else {
      if(USER$LoginPass >= 0) {
        login
      }
      else {
        loginfail
      }
    }
  })
}

shinyApp(ui, server)

任何关于如何让主体在加载时显示其中一个选项卡的建议将不胜感激。我怀疑这可能是由于 sidebarbody 的加载顺序所致,但我不确定如何进一步调查。

我也尝试过 conditionalPanel,但也无法正常工作。

更新 此屏幕截图显示了登录后立即发生的行为。

此屏幕截图显示了登录后所需的行为。

您需要将 tabItem 设置为活动状态:

tabItem(tabName = "t_item1", class = "active", box(title = "Item 1 information"))

登录后动态渲染内容时会渲染第一个tab