html - 如何在 Shiny 忙碌并显示加载文本时禁用所有操作按钮

在 Shiny 的应用程序中,我想在应用程序运行时禁用所有按钮。我有很多操作按钮、依赖项和一些 renderui 东西,所以我认为使用 shinyjs:disable(button) 是至关重要的,而且超过 40 个按钮非常不干净。

有没有一种简单的方法可以在 Shiny 的应用程序忙碌时禁用按钮(或一次禁用所有按钮/ slider ),例如在下面我的示例应用程序的“正在加载..”元素的情况下?

或者是否有另一种方法可以禁止所有按钮被点击或使它们在“正在加载..”文本指示的长时间计算运行时不可见?

在下面的示例中,我想在应用程序忙碌时禁用操作按钮(显示“正在加载..”文本)。我知道对于这个例子我可以使用 shinyjs 但我更喜欢在应用程序繁忙时使用整体解决方案。非常欢迎任何帮助,我对 html、css 和 java 完全陌生,所以如果有人知道解决方案,那么简短的解释会非常棒!

非常感谢!

library(shiny)


server <- function(input, output) {
  output$moreControls <- renderUI({if(input$obs!=10001) actionButton("button", "OK!")})
  observeEvent(input$button, {
  output$distPlot <- renderPlot({
    Sys.sleep(5)
    hist(rnorm(isolate(input$obs)), col = 'darkgray', border = 'white')
  })})
}

ui <- fluidPage(tags$head(tags$style(type="text/css", "
                                                                    #loadmessage {
                                     position: fixed;
                                     top: 95%;
                                     left: 0px;
                                     width: 100%;
                                     padding: 5px 0px 5px 0px;
                                     text-align: center;
                                     font-weight: bold;
                                     font-size: 100%;
                                     color: #000000;
                                     background-color: #CCFF66;
                                     z-index: 105;
                                     }
                                     ")),
                conditionalPanel(condition="$('html').hasClass('shiny-busy')",
                                 tags$div("Loading...",id="loadmessage")),
  sidebarLayout( sidebarPanel(

    sliderInput("obs", "Number of observations:", min = 10000, max = 100000, value = 10001,step=1000),
   uiOutput("moreControls")

    ),
    mainPanel(plotOutput("distPlot"))
  )
)

shinyApp(ui = ui, server = server) 

最佳答案

我不熟悉一种简单的方法来完成您所描述的事情,但这当然并不意味着没有 ;) 我相信这里有一个小的解决方法可以满足您的要求,并使您的代码保持相对干净。我们可以使用 reactiveValuesToList(input) 来获取我们的输入列表,然后编写一个函数来禁用或启用它们。我们还可以通过基于属性对列表进行子集化来决定仅切换 button 输入。

下面的工作示例,希望对您有所帮助!



library(shiny)
library(shinyjs)

ui <- fluidPage(
  h3('Disable all inputs while running'),
  actionButton('btn_all_inputs','Run long process'),
  h3('Disable only buttons while running'),
  actionButton('btn_only_buttons','Run long process'),
  hr(),
  h3('Inputs'),
  textInput('text1', 'Text1',"my text:"),
  actionButton('btn1','Button 1'),
  actionButton('btn2','Button 2'),
  actionButton('btn3','Button 3'),
  sliderInput('slid3','Slider 1',min=0,max=1,value=0.5),
  useShinyjs()
)


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

  # Function to toggle input elements. 
  # input_list: List of inputs, reactiveValuesToList(input)
  # enable_inputs: Enable or disable inputs?
  # Only buttons: Toggle all inputs, or only buttons?
  toggle_inputs <- function(input_list,enable_inputs=T,only_buttons=FALSE)
  {
    # Subset if only_buttons is TRUE.
    if(only_buttons){
      buttons <- which(sapply(input_list,function(x) {any(grepl('Button',attr(x,"class")))}))
      input_list = input_list[buttons]
    }

    # Toggle elements
    for(x in names(input_list))
      if(enable_inputs){
        shinyjs::enable(x)} else {
          shinyjs::disable(x) }
  }

  observeEvent(input$btn_all_inputs,{
    input_list <- reactiveValuesToList(input)
    toggle_inputs(input_list,F,F)
    Sys.sleep(5)
    toggle_inputs(input_list,T,F)
  })

  observeEvent(input$btn_only_buttons,{
    input_list <- reactiveValuesToList(input)
    toggle_inputs(input_list,F,T)
    Sys.sleep(5)
    toggle_inputs(input_list,T,T)
  })
}

shinyApp(ui = ui, server = server)

替代方案

此解决方案使用自定义 JavaScript 根据 Shiny 是忙还是闲来启用/禁用所有输入。因此,这将在 Shiny 忙碌时禁用您的输入。我现在将脚本设置为禁用所有按钮,但您可以通过向 document.getElementsByTagName() 添加更多选择来轻松扩展它。希望这更接近您的想法。

library(shiny)

ui <- fluidPage(
  h3('Disable buttons while running'),
  actionButton('btn_run','Run long process'),
  hr(),
  h3('Inputs'),
  textInput('text1', 'Text1',"my text:"),
  actionButton('btn1','Button 1'),
  sliderInput('slid3','Slider 1',min=0,max=1,value=0.5),
  includeScript('script.js')
)

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

  observeEvent(input$btn_run,{
    Sys.sleep(5)
  })
}

shinyApp(ui = ui, server = server)

脚本.js

$(document).on("shiny:busy", function() {
  var inputs = document.getElementsByTagName("button");
  console.log(inputs);
for (var i = 0; i < inputs.length; i++) {
inputs[i].disabled = true;
}
});

$(document).on("shiny:idle", function() {
  var inputs = document.getElementsByTagName("button");
  console.log(inputs);
for (var i = 0; i < inputs.length; i++) {
inputs[i].disabled = false;
}
});

https://stackoverflow.com/questions/48851729/

相关文章:

haskell - 如何强制评估 `unsafePerformIO` 内的 IO 操作?

r - 在 R 中生成所有可能的长度为 n 的二进制向量

php - Magento 2 的产品图片和缩略图显示行为

r - 在 R 中使用什么包进行 Kmeans 预测?

angular - 如何将样式添加到动态创建的组件 - angular 5

git - nix-prefetch-git sha256 要求对标签使用修订版

python - 如何使用 sklearn python 预测 future 的数据帧?

json - Swift 4 Decodable - 带有对象的数组

reactjs - 没有安装 react-leaflet 库

wordpress - WooCommerce REST API : query products