请注意,我在侧边栏菜单中拥有SELECTED = TRUE
标志名称,这种印象使我印象深刻是因为它是在渲染时加载的第一页,因此应将其设为“首页”。但是正如您所看到的,它不会这样做。输入用户名/密码(sam / 123)后,它将进入空白页,并且直到您选择该选项卡,该选项卡才会出现。
如果SELECTED = TRUE
标志实际上没有达到我的预期,那么获得所需输出的正确方法是什么?
UI:
library(shiny)
library(shinydashboard)
header <- dashboardheader(title = "x")
sidebar <- dashboardSidebar(uiOutput("sidebarpanel"))
body <- dashboardBody(uiOutput("body"))
ui <- dashboardPage(header,sidebar,body)
login_details <- data.frame(user = c("sam"),pswd = c("123"))
login <- box(
textInput("username","username"),passwordInput("passwd","Password"),actionButton("Login","Log in")
)
服务器:
server <- function(input,output,session) {
login.page = paste(
isolate(session$clientData$url_protocol),"//",isolate(session$clientData$url_hostname),":",isolate(session$clientData$url_port),sep = ""
)
histdata <- rnorm(500)
USER <- reactiveValues(Logged = F)
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(login_details$user %in% username)
Id.password <- which(login_details$pswd %in% Password)
if (length(Id.username) > 0 & length(Id.password) > 0){
if (Id.username == Id.password) {
USER$Logged <- TRUE
}
}
}
}
}
})
output$sidebarpanel <- renderUI({
if (USER$Logged == TRUE) {
div(
sidebarMenu(
menuItem(
"Item 1",tabName = "t_item1",icon = icon("line-chart"),selected = TRUE
)
)
)
}
})
output$body <- renderUI({
if (USER$Logged == TRUE) {
tabItems(
tabItem(tabName = "t_item1",fluidRow(
output$plot1 <- renderPlot({
data <- histdata[seq_len(input$slider)]
hist(data)
},height = 300,width = 300),box(
title = "Controls",sliderInput("slider","observations:",1,100,50)
)
))
)
} else {
login
}
})
}