当应用程序繁忙时,如何防止用户在闪亮的应用程序上执行任何操作

2024-04-26

我有一个复杂的闪亮应用程序,有很多输入、传单地图等...... 我遇到的问题是,当应用程序忙于进行一些计算时,用户不断单击应用程序上的任意位置,有时应用程序会崩溃。

我想阻止用户在应用程序繁忙时进行任何点击。重要的是,旋转器保持旋转器状态,而不是像在waiter包裹。也许有可能将旋转器和服务员结合起来?但我还不知道如何做。

我这里有一个小代表: 当我单击“繁忙应用程序”按钮时,会出现一个 5 秒的旋转窗口,让用户等待。但在此期间,用户仍然可以点击“增量”按钮。在旋转器的末端,输出会随着点击次数的增加而增加。 我想立即阻止用户访问整个应用程序,而不仅仅是在应用程序繁忙时在按钮上放置“禁用”(因为我的应用程序中有很多输入,这需要太多修改)

library(shiny)

ui <- fluidPage(

  # spinner css
  tags$head(
    tags$style(HTML("
  #loadmessage {
  position:fixed; z-index:8; top:50%; left:50%; padding:10px;
  text-align:center; font-weight:bold; color:#000000; background-color:#CCFF66;
  }
  
  .loader {
  position:fixed; z-index:8; border:16px solid #999999;
  border-top: 16px solid #8B0000; border-radius: 50%;
  width: 120px; height: 120px; top:45%; left:45%;
  animation: spin 2s linear infinite;
  }

  @keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
  }"))
  ),
  
  # display load spinner when shiny is busy
  conditionalPanel(
    condition = "$(\'html\').hasClass(\'shiny-busy\')",
    tags$div(class = "loader")
  ),
  actionButton(
    inputId = "increment",
    label = "Increment"
  ),
  textOutput("result"),
  actionButton(
    inputId = "busy",
    label = "Busy app"
  )
)

server <- function(input, output, session) {
  
  rv <- reactiveValues(counter = 0)

  #increment counter
  observeEvent(input$increment,{
    rv$counter = rv$counter + 1
  })
  
  #display incremented counter
  output$result <- renderText({
    rv$counter
  })
  
  observeEvent(input$busy, {
    Sys.sleep(5)
    # during this time, the user should not be able to do anything on the app
  })
}

shinyApp(ui = ui, server = server)

在分析了您的答案并进行了更多思考之后,我想我找到了最简单的解决方案。

在“闪亮繁忙”事件中,我在条件面板中显示一个 div,它是屏幕的 100%,并且在第一个计划中,因此它可以防止对其后面的输入/输出进行任何点击。当应用程序不再繁忙时,面板就会消失。该面板是透明的,因此用户看不到它。

此外,它使我能够禁用所有输入和输出,而不依赖于计时器,仅取决于应用程序是否繁忙。

library(shiny)

ui <- fluidPage(

  # spinner css
  tags$head(
    tags$style(HTML("
  #loadmessage {
  position:fixed; z-index:8; top:50%; left:50%; padding:10px;
  text-align:center; font-weight:bold; color:#000000; background-color:#CCFF66;
  }
  
  .loader {
  position:fixed; z-index:8; border:16px solid #999999;
  border-top: 16px solid #8B0000; border-radius: 50%;
  width: 120px; height: 120px; top:45%; left:45%;
  animation: spin 2s linear infinite;
  }

  .prevent_click{
  position:fixed; 
  z-index:9;
  width:100%;
  height:100vh;
  background-color: transpare'nt;   
  }

  @keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
  }"))
  ),
  
  # display load spinner when shiny is busy
  conditionalPanel(
    condition = "$(\'html\').hasClass(\'shiny-busy\')",
    tags$div(class = "loader"),
    tags$div(class = "prevent_click")
  ),
  actionButton(
    inputId = "increment",
    label = "Increment"
  ),
  textOutput("result"),
  actionButton(
    inputId = "busy",
    label = "Busy app"
  )
)

server <- function(input, output, session) {
  
  rv <- reactiveValues(counter = 0)

  #increment counter
  observeEvent(input$increment,{
    rv$counter = rv$counter + 1
  })
  
  #display incremented counter
  output$result <- renderText({
    rv$counter
  })
  
  observeEvent(input$busy, {
    Sys.sleep(5)
    # during this time, the user should not be able to do anything on the app
  })
}

shinyApp(ui = ui, server = server)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当应用程序繁忙时,如何防止用户在闪亮的应用程序上执行任何操作 的相关文章

随机推荐

Powered by Hwhale