闪亮应用中的不同页面 [英] Different pages in Shiny App

查看:9
本文介绍了闪亮应用中的不同页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在我闪亮的仪表板中有不同的页面。首先,我创建了一个登录页面来对用户和管理员进行身份验证。之后,如果管理员登录到系统,希望看到一些选项,用户无法访问它们。 问:当我以用户或管理员身份登录时,我可以在后台看到主要的ui.r页面,如何解决此问题才能只看到admin.R或user.R? 用户登录时会显示仪表板,管理员登录时会显示仪表板和小部件。 因此,我决定在R中创建4个页面,如下所示: ui.R

library(shiny)
library(shinydashboard)
shinyUI( 
  dashboardPage(
    dashboardHeader(title = "Navigational Support System"),
    dashboardSidebar(),
    dashboardBody(
      box(
        uiOutput("page")
      )
    )
  )
)

server.R

library(shiny)
library(shinydashboard)
source("user.R")
source("admin.R")
############################################################################################################
#Login USER and ADMIN TO the System
my_username <- c("test","admin")
my_password <- c("test","123")
get_role=function(user){
  if(user=="test") {
    return("TEST")
  }else{
    return("ADMIN")
  }
}

get_ui=function(role){
  if(role=="TEST"){
    return(list_field_user)
  }else{
    return(list_field_admin)
  }
}


shinyServer(function(input, output,session) {

  USER <- reactiveValues(Logged = FALSE,role=NULL)

  ui1 <- function(){
    tagList(
      div(id = "login",
          wellPanel(textInput("userName", "Username"),
                    passwordInput("passwd", "Password"),
                    br(),actionButton("Login", "Log in")))
      #tags$style(type="text/css", '#login{ width:750px; float:left;}')

    )}

  ui2 <- function(){tagList(tabPanel("NSS",get_ui(USER$role)))}

  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)
          Id.password <- which(my_password == Password)
          if (length(Id.username) > 0 & length(Id.password) > 0) {
            if (Id.username == Id.password) {
              USER$Logged <- TRUE
              USER$role=get_role(Username)

            }
          } 
        }
      }
    }
  })
  observe({
    if (USER$Logged == FALSE) {

      output$page <- renderUI({
        div(class="outer",do.call(bootstrapPage,c("Please Login",ui1())))
      })
    }
    if (USER$Logged == TRUE)    {
      output$page <- renderUI({
        div(class="outer",do.call(navbarPage,c(inverse=TRUE,title = "Welcome Admin!",ui2())))
      })
      #print(ui)
    }
  })
  ##################################################################################################

})

管理员。r

list_field_admin =

  shinyUI( 
  dashboardPage(
    dashboardHeader(title = "Decison Support System"),
    dashboardSidebar( sidebarMenu(
      menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
      menuItem("Widgets", tabName = "widgets", icon = icon("th"))
    )
    ),
    dashboardBody(
  )

))

用户。r

list_field_user =  shinyUI( 
  dashboardPage(
    dashboardHeader(title = "Decison Support System"),
    dashboardSidebar( sidebarMenu(
      menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard"))

    )
    ),
    dashboardBody(
    )

  ))

推荐答案

如果我理解,您可以在其他文件中创建不同的列表

1)管理员。r

admin_title="Decison Support System"
admin_side=list(sidebarMenu(
  menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
  menuItem("Widgets", tabName = "widgets", icon = icon("th"))
))
admin_main=list(

  tabItems(
    tabItem(tabName = "dashboard", list(h1("1234"),h2("234"))),
    tabItem(tabName = "widgets", list(fluidRow(column(6,numericInput("inputtest", "test", value = 0),column(6,actionButton(inputId ="test1",label ="go")))))
  )
  ))

2)用户。r

test_title="Decison Support System"
test_side=list(sidebarMenu(
  menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard"))))

test_main=list(

  tabItems(
    tabItem(tabName = "dashboard", list(h1("user"),h2("user")))
  ))

然后稍微更改您的UI和服务器

UI:

library(shiny)
library(shinydashboard)
shinyUI( 
  dashboardPage(
    dashboardHeader( title=textOutput("title")),
    dashboardSidebar(uiOutput("side")),
    dashboardBody(
          uiOutput("page")

    )
  )

)

服务器:

library(shiny)
library(shinydashboard)
source("user.R")
source("admin.R")

my_username <- c("test","admin")
my_password <- c("test","123")
get_role=function(user){

  if(user=="test") {

    return("TEST")
  }else{

    return("ADMIN")
  }
}

get_ui=function(role){
  itog=list()
  if(role=="TEST"){
    itog$title=test_title
    itog$main=test_main
    itog$side=test_side
    return(itog)
  }else{
    itog$title=admin_title
    itog$main=admin_main
    itog$side=admin_side
    return(itog)
  }
}


shinyServer(function(input, output,session) {

  USER <- reactiveValues(Logged = FALSE,role=NULL)

  ui1 <- function(){
    tagList(
      div(id = "login",
          wellPanel(textInput("userName", "Username"),
                    passwordInput("passwd", "Password"),
                    br(),actionButton("Login", "Log in")))
      ,tags$style(type="text/css", "#login {font-size:10px;   text-align: left;position:absolute;top: 40%;left: 50%;margin-top: -10px;margin-left: -150px;}")
    )}


  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)
          Id.password <- which(my_password == Password)
          if (length(Id.username) > 0 & length(Id.password) > 0) {
            if (Id.username == Id.password) {
              USER$Logged <- TRUE
              USER$role=get_role(Username)

          }
        } 
      }
    }
    }
  })
  observe({
    if (USER$Logged == FALSE) {

      output$page <- renderUI({
        box(
        div(class="outer",do.call(bootstrapPage,c("",ui1()))))
      })
    }
    if (USER$Logged == TRUE)    {
      itog=get_ui(USER$role)
      output$title<- renderText({
        itog$title
      })
      output$side <- renderUI({
        itog$side
      })
      output$page <- renderUI({
        itog$main
      })
      }
  })
})

这篇关于闪亮应用中的不同页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆