如何访问Shinyalerts使用的Shiny标签ID?

时间:2019-05-13 22:22:27

标签: r shiny passwords shinydashboard

我正在尝试为用户设置一个非常基本的块,除非他们输入密码,否则他们将无法从tabPanel访问选项卡。我正在为此使用shinyalerts,到目前为止,它可以作为临时密码系统使用。

但是,当我尝试将其设置为观察我的代码中选择panel_2的事件时,它只是在应用程序的开头弹出。当我单击panel_2时,它确实会响应,但是如何打开打开应用程序且仅在input$tab == "panel_2"为真时才停止弹出呢?

我能够看到observeserver中打印面板ID,所以我知道它存在。

注意:我正在使用shinyalerts的github回购版本

library(shiny)
library(shinyalert)
library(shinydashboardPlus)

#password accept function
password_accept = function(x){
  if(x =='1234'){
    shinyalert('Welcome Administrator')
  } else {
    shinyalert("TRY AGAIN",
               "Enter Password for Admin Access to this Page"
               ,type = "input"
               ,inputType = "password"
               ,time = 0
               #,inputValue = "Enter Password"
               ,callbackR = password_accept
    )
  }
}


#example of problem
ui = fluidPage(#useShinyalert(),
  navbarPage("Sample",
             id = 'tabs',
             tabPanel("panel1", useShinydashboardPlus(),
                      fluidRow(column(9,offset = 1,
                                      h3(htmlOutput("sample app")))
                               ),
                      column(4,offset = 5,
                             boxPlus(solidHeader = T, collapsible = F, collapsed = F, closable = F, title = '', status = 'success',
                                     uiOutput('fn'),br(),
                                     uiOutput('ln'),br() 
                             ))
                      ),
             tabPanel("panel2",useShinyalert(), # add shiny alert to act as pwd signin
                      column(6,
                             fluidRow(
                               ))
             )))


server = (function(input, output,session) {

  #print the tab being accessed
  observe({print(input$tabs)})

  #admin pop up
  observeEvent(input$tabs=='panel2', 
               {
                 shinyalert("",
                            "Enter Password for Admin Access to this Page"
                            ,type = "input"
                            ,inputType = "password"
                            ,time = 0
                            #,inputValue = "Enter Password"
                            ,callbackR = password_accept
                 )
               }
  )

  output$fn = renderUI({
    textInput(inputId = "first_name", label = "First Name")
  })

  output$ln = renderUI({
    textInput(inputId = "last_name", label = "Last Name")
  })
})

shinyApp(ui,server)

1 个答案:

答案 0 :(得分:0)

您需要为ignoreInit = TRUE设置observeEvent

library(shiny)
library(shinyalert)
library(shinydashboardPlus)
library(shinyWidgets)

#password accept function
password_accept = function(x){
  if(x =='1234'){
    shinyalert('Welcome Administrator')
  } else {
    shinyalert("TRY AGAIN", 
               "Enter Password for Admin Access to this Page"
               , type = "input"
               , inputType = "password"
               , time = 0
               #, inputValue = "Enter Password"
               , callbackR = password_accept
    )
  }
}


#example of problem
ui = fluidPage(#useShinyalert(), 
  navbarPage("Sample", 
             id = 'tabs', 
             tabPanel("panel1", useShinydashboardPlus(), 
                      fluidRow(column(9, offset = 1, 
                                      h3(htmlOutput("sample app")))
                      ), 
                      column(4, offset = 5, 
                             boxPlus(solidHeader = T, collapsible = F, collapsed = F, closable = F, title = '', status = 'success', 
                                     uiOutput('fn'), br(), 
                                     uiOutput('ln'), br() 
                             ))
             ), 
             tabPanel("panel2", useShinyalert(), # add shiny alert to act as pwd signin
                      column(6, 
                             fluidRow(
                             ))
             )))


server = (function(input, output, session) {

  #print the tab being accessed
  observe({print(input$tabs)})

  #admin pop up
  observeEvent(input$tabs, 
               {
                 if(input$tabs == 'panel2'){
                   shinyalert("", 
                              "Enter Password for Admin Access to this Page"
                              , type = "input"
                              , inputType = "password"
                              , time = 0
                              #, inputValue = "Enter Password"
                              , callbackR = password_accept
                   )
                 }

               }, ignoreInit = TRUE
  )

  output$fn = renderUI({
    textInput(inputId = "first_name", label = "First Name")
  })

  output$ln = renderUI({
    textInput(inputId = "last_name", label = "Last Name")
  })
})

shinyApp(ui, server)