我正在使用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)
有关如何让主体在加载时显示其中一个标签的任何建议都将非常感激。我怀疑它可能是由sidebar
和body
的加载顺序引起的,但我不确定如何进一步调查。
我也试过了conditionalPanel
,但无法做到这一点。
答案 0 :(得分:6)
您需要将tabItem
设置为有效:
tabItem(tabName = "t_item1", class = "active", box(title = "Item 1 information"))
在您登录后动态呈现内容时,它将呈现第一个标签。