登录到闪亮的仪表板 R 后如何不空边
How to don't get empty side after login to shiny dashboard R
我是问题。我创建了带有登录面板的闪亮仪表板,每个角色都有不同的浏览选项。登录后,我得到一个空白页面,只有在选择 sidebarMenu 后我才能看到内容。我的意图是登录后立即从sidebarMenu列表中获取第一个的内容。有人知道怎么做吗?
下面附上我的应用程序的演示代码:
library(shinydashboard)
library(shiny)
Logged = FALSE
my_username <- c("test", "test2")
my_password <- c("test", "test2")
my_role <- c("Admin", "Guest")
Login_side <- function(){
sidebarMenu(id = "tabs")
}
Login_page <- function(){
tagList(
div(id = "login",
fluidRow(br()),
fluidRow(br()),
fluidRow(br()),
fluidRow(br()),
fluidRow(br()),
fluidRow(br()),
fluidRow(br()),
fluidRow(br()),
fluidRow(
column(4, offset = 4, uiOutput("Login_full_box"))
)
)
)
}
Admin_side <- function(){
sidebarMenu(id = "sbMenu",
menuItem("test menu", tabName = "test", icon = icon("th"))
)
}
Admin_page <- function(){
tabItems(tabItem(tabName = "test", h2("HELLO")))
}
Guest_side <- function(){
sidebarMenu(id = "sbMenu",
menuItem("test menu 2", tabName = "test2", icon = icon("th"))
)
}
Guest_page <- function(){
tabItems(tabItem(tabName = "test2", h2("HELLO 2")))
}
ui <- shinyUI(
dashboardPage(
dashboardHeader(title = "Dashboard", titleWidth = 190),
dashboardSidebar(width = 190, uiOutput("side")),
dashboardBody(height=1000, uiOutput("page"))
)
)
server <- function(input, output) {
USER <- reactiveValues(Logged = FALSE)
observe({
if (USER$Logged == FALSE){
if(!is.null(input$Login)){
if(input$Login > 0){
Username <- isolate(input$username)
Password <- isolate(input$password)
if(length(Username) > 0 && length(Password) > 0){
if(my_password[which(my_username==Username)]==Password){
USER$Logged <- TRUE
USER$role <- my_role[which(my_username==Username)]
}
else {
USER$Logged <- FALSE
}
}
else {
USER$Logged <- FALSE
}
}
}
}
})
observe({
if(USER$Logged == FALSE) {
output$side <- renderUI({Login_side()})
output$page <- renderUI({Login_page()})
}
if((USER$Logged == TRUE)){
if(USER$role == "Admin"){
output$side <- renderUI({Admin_side()})
output$page <- renderUI({Admin_page()})
}
if(USER$role == "Guest"){
output$side <- renderUI({Guest_side()})
output$page <- renderUI({Guest_page()})
}
}
})
output$Login_full_box <- renderUI({
box(title = "Login Panel",
width = 12,
status = "info",
solidHeader = TRUE,
fluidRow(
column(3, "Username:"),
column(9, textInput("username", label = NULL))
),
fluidRow(
column(3, "Password:"),
column(9, passwordInput("password", label = NULL))
),
fluidRow(
column(5, offset = 3, actionButton("Login", "Login"))
)
)
})
}
shinyApp(ui = ui, server = server)
将您的 tabName
更新为同名(它们永远不会同时存在):
Admin_side <- function(){
sidebarMenu(id = "sbMenu",
menuItem("test menu", tabName = "test", icon = icon("th"), selected = TRUE)
)
}
Guest_side <- function(){
sidebarMenu(id = "sbMenu",
menuItem("test menu 2", tabName = "test", icon = icon("th"))
)
}
然后将您的登录观察者更改为 observeEvent
并添加 updateTabsetPanel
:
observeEvent(input$Login, {
if(USER$Logged == FALSE) {
output$side <- renderUI({Login_side()})
output$page <- renderUI({Login_page()})
}
if((USER$Logged == TRUE)){
if(USER$role == "Admin"){
output$side <- renderUI({Admin_side()})
output$page <- renderUI({Admin_page()})
}
if(USER$role == "Guest"){
output$side <- renderUI({Guest_side()})
output$page <- renderUI({Guest_page()})
}
updateTabsetPanel(session, "sbMenu", selected = "test")
}
})
我是问题。我创建了带有登录面板的闪亮仪表板,每个角色都有不同的浏览选项。登录后,我得到一个空白页面,只有在选择 sidebarMenu 后我才能看到内容。我的意图是登录后立即从sidebarMenu列表中获取第一个的内容。有人知道怎么做吗?
下面附上我的应用程序的演示代码:
library(shinydashboard)
library(shiny)
Logged = FALSE
my_username <- c("test", "test2")
my_password <- c("test", "test2")
my_role <- c("Admin", "Guest")
Login_side <- function(){
sidebarMenu(id = "tabs")
}
Login_page <- function(){
tagList(
div(id = "login",
fluidRow(br()),
fluidRow(br()),
fluidRow(br()),
fluidRow(br()),
fluidRow(br()),
fluidRow(br()),
fluidRow(br()),
fluidRow(br()),
fluidRow(
column(4, offset = 4, uiOutput("Login_full_box"))
)
)
)
}
Admin_side <- function(){
sidebarMenu(id = "sbMenu",
menuItem("test menu", tabName = "test", icon = icon("th"))
)
}
Admin_page <- function(){
tabItems(tabItem(tabName = "test", h2("HELLO")))
}
Guest_side <- function(){
sidebarMenu(id = "sbMenu",
menuItem("test menu 2", tabName = "test2", icon = icon("th"))
)
}
Guest_page <- function(){
tabItems(tabItem(tabName = "test2", h2("HELLO 2")))
}
ui <- shinyUI(
dashboardPage(
dashboardHeader(title = "Dashboard", titleWidth = 190),
dashboardSidebar(width = 190, uiOutput("side")),
dashboardBody(height=1000, uiOutput("page"))
)
)
server <- function(input, output) {
USER <- reactiveValues(Logged = FALSE)
observe({
if (USER$Logged == FALSE){
if(!is.null(input$Login)){
if(input$Login > 0){
Username <- isolate(input$username)
Password <- isolate(input$password)
if(length(Username) > 0 && length(Password) > 0){
if(my_password[which(my_username==Username)]==Password){
USER$Logged <- TRUE
USER$role <- my_role[which(my_username==Username)]
}
else {
USER$Logged <- FALSE
}
}
else {
USER$Logged <- FALSE
}
}
}
}
})
observe({
if(USER$Logged == FALSE) {
output$side <- renderUI({Login_side()})
output$page <- renderUI({Login_page()})
}
if((USER$Logged == TRUE)){
if(USER$role == "Admin"){
output$side <- renderUI({Admin_side()})
output$page <- renderUI({Admin_page()})
}
if(USER$role == "Guest"){
output$side <- renderUI({Guest_side()})
output$page <- renderUI({Guest_page()})
}
}
})
output$Login_full_box <- renderUI({
box(title = "Login Panel",
width = 12,
status = "info",
solidHeader = TRUE,
fluidRow(
column(3, "Username:"),
column(9, textInput("username", label = NULL))
),
fluidRow(
column(3, "Password:"),
column(9, passwordInput("password", label = NULL))
),
fluidRow(
column(5, offset = 3, actionButton("Login", "Login"))
)
)
})
}
shinyApp(ui = ui, server = server)
将您的 tabName
更新为同名(它们永远不会同时存在):
Admin_side <- function(){
sidebarMenu(id = "sbMenu",
menuItem("test menu", tabName = "test", icon = icon("th"), selected = TRUE)
)
}
Guest_side <- function(){
sidebarMenu(id = "sbMenu",
menuItem("test menu 2", tabName = "test", icon = icon("th"))
)
}
然后将您的登录观察者更改为 observeEvent
并添加 updateTabsetPanel
:
observeEvent(input$Login, {
if(USER$Logged == FALSE) {
output$side <- renderUI({Login_side()})
output$page <- renderUI({Login_page()})
}
if((USER$Logged == TRUE)){
if(USER$role == "Admin"){
output$side <- renderUI({Admin_side()})
output$page <- renderUI({Admin_page()})
}
if(USER$role == "Guest"){
output$side <- renderUI({Guest_side()})
output$page <- renderUI({Guest_page()})
}
updateTabsetPanel(session, "sbMenu", selected = "test")
}
})