【发布时间】:2020-03-04 19:00:35
【问题描述】:
请注意,我在侧边栏菜单中有 SELECTED = TRUE 标志指定,我的印象是假设该选项卡成为“主页”,因为它是渲染时加载的第一页。但如您所见,它并没有这样做。输入用户名/密码 (sam/123) 后,它会进入一个空白页面,并且该选项卡在您选择之前不会出现。
如果 SELECTED = TRUE 标志实际上没有按照我的想法进行,那么获得所需输出的正确方法是什么?
用户界面:
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
}
})
}
【问题讨论】:
标签: r shiny shinydashboard shiny-server shinyapps