我正在使用 R/shinydasboard 创建一个我放在登录屏幕后面的 Web 应用程序。
我无法根据侧边栏菜单选项卡渲染主体。
我试图确保其中一个选项卡项目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
但是也无法正常工作。