简体   繁体   English

Shiny 中受密码保护的仪表板选项卡

[英]Password protected dashboard tabs in Shiny

I am trying adapt Paul Campbell's shinyauthr example https://paul.rbind.io/2018/11/04/introducing-shinyauthr/我正在尝试改编 Paul Campbell 的 shinyauthr 示例https://paul.rbind.io/2018/11/04/introducing-shinyauthr/

to create a shiny dashboard with password authentication.创建一个带有密码身份验证的闪亮仪表板。 I have tried a number of options and I have read questions about similar problems but I have been unable to adapt them to solve my issue.我尝试了多种选择,并且阅读了有关类似问题的问题,但我无法调整它们来解决我的问题。 I can require a password to access the dashboard but I cannot cannot put things into the dashboard tabs.我可以要求输入密码才能访问仪表板,但我无法将内容放入仪表板选项卡。

This is my minimum working example which tries to extend Paul's example.这是我试图扩展 Paul 示例的最小工作示例。

library(shiny)
library(shinyauthr)
library(shinyjs)
library(shinydashboard)

# dataframe that holds usernames, passwords and other user data
user_base <- data.frame(
  user = c("user1", "user2"),
  password = c("pass1", "pass2"), 
  permissions = c("admin", "standard"),
  name = c("User One", "User Two"),
  stringsAsFactors = FALSE
)

ui <-dashboardPage(
  dashboardHeader(title = "Hello"),
  dashboardSidebar(
    sidebarMenu("Select Security", tabName = "select_security"),
    sidebarMenu("Portfolio", tabName = "portfolio")
  ),
  dashboardBody(

  shinyjs::useShinyjs(),
  div(class = "pull-right", shinyauthr::logoutUI(id = "logout")),
  shinyauthr::loginUI(id = "login"),
  tags$div(tabName = "portfolio", tableOutput("user_table"), 
           class = "tab_content"), 
  tags$div(tabName = "select_security", textOutput("welcome_note"), class = "tab_content")
)
)

server <- function(input, output, session) {

  logout_init <- callModule(shinyauthr::logout, 
                            id = "logout", 
                            active = reactive(credentials()$user_auth))

  credentials <- callModule(shinyauthr::login, 
                            id = "login", 
                            data = user_base,
                            user_col = user,
                            pwd_col = password,
                            log_out = reactive(logout_init()))

    output$user_table <- renderTable({
    req(credentials()$user_auth)
    user_data()
    })
  output$welcome_note <- renderText({
  req(credentials()$user_auth)
  print("Hello")
})

} 

shinyApp(ui = ui, server = server)

The Hello arrives with the table but I want it to be attached to the 'Portfolio' tab. Hello 随表格一起到达,但我希望将其附加到“投资组合”选项卡。 I am using the tags$div method here because the standard dashboard menuItem approach did not work so I followed this advice: Using shiny modules and shinydashboard: shiny.tag error我在这里使用tags$div方法,因为标准仪表板menuItem方法不起作用,所以我遵循了这个建议: Using shiny modules and shinydashboard: shiny.tag error

Rob

I think that I solved it and I think that this is the answer.我认为我解决了它,我认为这就是答案。 Each tab has its own input and that is only revealed when the password is supplied.每个选项卡都有自己的输入,只有在提供密码时才会显示。

library(shiny)
library(shinyauthr)
library(shinyjs)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "My Dashboard",

  dashboardSidebar( 
  shinyjs::useShinyjs(),
  sidebarMenu(
    menuItem("Security Selection", tabName = "security_selection", icon = icon("dashboard")), 
      menuItem("Portfolio", tabName = "portfolio", icon = icon("th"))
       ), 


  dashboardBody(
    useShinyjs(),
    tags$head(tags$style(".table{margin: 0 auto;}"),

              tags$script(src="https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/3.5.16/iframeResizer.contentWindow.min.js",
                          type="text/javascript"),
              includeScript("returnClick.js")
    ),
    shinyauthr::loginUI("login"),
    tabItems(
      # first tab content
    tabItem(tabName = "security_selection", uiOutput("security_selection")),
    tabItem(tabName = "portfolio", uiOutput("portfolio"))

    )
)
)


server <- function(input, output, session) {

  credentials <- callModule(shinyauthr::login, "login", 
                            data = user_base,
                            user_col = user,
                            pwd_col = password_hash,
                            sodium_hashed = TRUE,
                            log_out = reactive(logout_init()))

  logout_init <- callModule(shinyauthr::logout, "logout", reactive(credentials()$user_auth))


  user_info <- reactive({credentials()$info})

  user_data <- reactive({
    req(credentials()$user_auth)


   output$security_selection <- renderUI({
    req(credentials()$user_auth)
    tagList(selectInput("name", "Name", choice = "user1", selected = "user1"),
                 selectInput("asset", "Asset", choice = c("Equity", "Debt", "Gold", "BTC"),
                              selected = "SPY"),
                  selectInput("action", "Action", choice = c("Buy", "Sell"), selected = "Buy"),
                  numericInput("quantity", "Assets to buy or sell", value = 100, min = 0, 
                               max = 10e6),
                  actionButton("submit", "Submit")
    )  

  })

   output$portfolio <- renderUI({
    req(credentials()$user_auth)
    tabItem("portfolow", 
    tags$p("THis is a note")
)
})

  }

shiny::shinyApp(ui, server)

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

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