如何访问 Shiny 标签 ID 以与 Shinyalerts 一起使用? [英] How to access Shiny tab ids for use with shinyalerts?

查看:66
本文介绍了如何访问 Shiny 标签 ID 以与 Shinyalerts 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为用户设置一个非常基本的块,除非他们输入密码,否则他们将无法从 tabPanel 访问选项卡.我为此使用了 shinyalerts,到目前为止,它在充当临时密码系统方面运行良好.

I am trying to set up a very basic block for users where they won't be able to access a tab from tabPanel unless they enter a password. I am using shinyalerts for this and so far it works perfectly in terms of acting as an ad-hoc password system.

但是当我尝试将它设置为观察我的代码中选择 panel_2 的事件时,它只是在应用程序的开头弹出.当我点击 panel_2 时它确实有响应,但是我如何让它在我打开应用程序后立即停止弹出,并且只有当 input$tab == "panel_2"是真的吗?

But when I try to set it to observe the event of the selection of panel_2 in my code, it simply pops up in the beginning of the app. It does respond when I click panel_2 but how do I get it to stop popping up as soon as I open the app and only when the input$tab == "panel_2" is true?

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

I am able to see observe print the panel id in server so I know it exists.

注意:我使用的是 shinyalerts

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)

推荐答案

你需要为你的observeEvent设置ignoreInit = TRUE:

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)

这篇关于如何访问 Shiny 标签 ID 以与 Shinyalerts 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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