简体   繁体   English

Shinydashboard - 根据登录信息和选择输入显示或隐藏选项卡

[英]Shinydashboard - show or hide tab based on log in info AND selectinput

I'm working on a shinydashboard that is secured using shinymanager and I am having trouble.我正在使用 shinymanager 保护的 shinydashboard 上工作,但我遇到了麻烦。 I want to make a tab appear or disappear based on two bits of information.我想根据两位信息使选项卡出现或消失。 I have saved in my shinymanager credentials a user code that says what their home organization is.我在我的 shinymanager 凭据中保存了一个用户代码,说明他们的家庭组织是什么。 In my app I also have a selectInput that allows the user to choose a particular user code.在我的应用程序中,我还有一个 selectInput 允许用户选择特定的用户代码。 I have tabs that I would like to show only if the user code for that user's credentials match the selected user code from selectInput.我有一些选项卡,只有当该用户凭据的用户代码与从 selectInput 中选择的用户代码匹配时,我才想显示这些选项卡。 So for example, in the below example, if I had in res_auth a field called 'unit' with the choices aa, bb, cc, dd, ee - and the user who logs in is in unit aa, and they choose aa from the selectInput, then the iris tabs would show up - but if they choose bb they would not see the iris tabs.因此,例如,在下面的示例中,如果我在 res_auth 中有一个名为“unit”的字段,其选项为 aa、bb、cc、dd、ee - 并且登录的用户在 unit aa 中,他们从selectInput,然后 iris 选项卡就会出现 - 但如果他们选择 bb,他们将看不到 iris 选项卡。

library(shiny)
library(shinythemes)
library(shinymanager)
library(shinydashboard)
library(tidyverse)

options(warn=-1)
data(iris)
data(mtcars)

tabset1 = tabsetPanel(id = "mtcars",
                            tabPanel(id = "mtplots","mtcars plots",
                                     fluidRow(box(title = "Plot1", plotOutput("mtcarsplot1"))
                            )),
                            
                            
                            tabPanel(id = "mttable","MTcars tables",
                                     fluidRow(box(title = "Table 1",  tableOutput("mtcarstable1")))
                            ))



tabset2 = tabsetPanel(id = "iris",
                      tabPanel(id = "iris","iris plots",
                               fluidRow(box(title = "Plot1", plotOutput("irisplot1"))
                               )),
                      
                      
                      tabPanel(id = "mttable","iris tables",
                               fluidRow(box(title = "Table 1",  tableOutput("iristable1")))
                      ))




# Define UI for application that draws a histogram
ui <- dashboardPage(

  
  dashboardHeader(),
  
  
  dashboardSidebar(
    sidebarMenu(
    selectInput("which unit", "Choose a unit", choices = c("aa", "bb", "cc", "dd")),
    menuItem("MTCARS", tabName = "mt", icon = icon("user-tie")),
    selectInput("mtvar", "Choose a variable", choices = colnames(mtcars)),
    
#    RIGHT HERE
    
    menuItem("IRIS", icon = icon("envelope-open-text"), tabName = "ir"),
    
    selectInput("irvar", "Choose a variable", choices = colnames(iris))
    )
  ),
  
  dashboardBody(
    tabItems(
      tabItem("ir", tabset2),
      tabItem("mt", tabset1)
      )
      
    )
  )




ui <- secure_app(ui, enable_admin = TRUE)


# Begin Server ----------------------------------------------

server <- function(input, output, session) {
  
  res_auth <- secure_server(
    check_credentials = check_credentials("mycredentials.sqlite")
  )
  
  output$mtcarsplot1=renderPlot({
    
    
    ggplot(mtcars, aes_string(x = input$mtvar)) + stat_bin(nbins = input$irislines)
    
    
  })
  
  output$irisplot1=renderPlot({
    ggplot(iris, aes_string(x = input$irvar)) + stat_bin(nbins = input$mtlines)
    
    
  })
  
  
  output$mtcarstable1=renderTable({
    head(mtcars, input$mtlines)
    
  })
 
  
  output$iristable1=renderTable({
    head(iris, input$irislines)
    
  })
  
  
  
  
}

shinyApp(ui, server)

First replace the line below # RIGHT HERE with:首先将下面的# RIGHT HERE行替换为:

uiOutput("test"),

Then add a function like this to the server:然后向服务器添加一个这样的function:

output$test <- renderUI({
        if(input$`which unit`=="aa") {
            menuItem("IRIS", icon = icon("envelope-open-text"), tabName = "ir")
        }
    })

You will need to add a condition to the if to require their credentials to match in your situation, but this is essentially what you want.您将需要向if添加一个条件,以要求他们的凭据与您的情况相匹配,但这基本上就是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM