忙碌时禁用selectInput和菜单

时间:2018-10-08 07:03:13

标签: javascript r shiny

我想在加载Shiny应用程序时禁用菜单项并选择input。我设法用一些Javacript禁用了按钮和textInput(参见Disable elements when Shiny is busy),但是我无法使其与selectInput和菜单一起使用。我对替代解决方案不感兴趣。

library(shiny)

js <- "$(document).on('shiny:busy', function() {
  var $inputs = $('button,input,select,ul');
  console.log($inputs);
  $inputs.prop('disabled', true);
});

$(document).on('shiny:idle', function() {
  var $inputs = $('button,input,select,ul');
  console.log($inputs);
  $inputs.prop('disabled', false);
});"


ui <- fluidPage(

  titlePanel("Hello Shiny!"),

  sidebarLayout(
    sidebarPanel(
      tags$head(tags$script(js)),
      navlistPanel(
        tabPanel("Component 1"),
        tabPanel("Component 2")
      )
    ),
    mainPanel(
      actionButton("buttonID","This adds 10 seconds of Sys.sleep"),
      textInput("textID","Write text here..."),
      selectInput("selectID","This should be disables while loading",choices=c("A","B","C"))
    )
  )
)

server <- function(input, output) { 
  observeEvent(input$buttonID,{
    Sys.sleep(10) 
  })
}

shinyApp(ui, server)

1 个答案:

答案 0 :(得分:2)

使用shinyjs软件包可以更轻松地禁用小部件。那里有一个reactiveValuesToList函数,该函数将收集session中所有的反应输入,您可以简单地使用它:

library(shiny)
library(shinyjs)

ui <- fluidPage(

  titlePanel("Hello Shiny!"),
  useShinyjs(), 
  sidebarLayout(
    sidebarPanel(
      navlistPanel(
        tabPanel("Component 1"),
        tabPanel("Component 2")
      )
    ),
    mainPanel(
      actionButton("buttonID","This adds 5 seconds of Sys.sleep"),
      textInput("textID","Write text here..."),
      selectInput("selectID","This should be disables while loading",choices=c("A","B","C"))
    )
  )
)

server <- function(input, output) { 

  observeEvent(input$buttonID,{
    myinputs <- names(reactiveValuesToList(input))
    print(myinputs)
    for(i in 1:length(myinputs)){
      disable(myinputs[i])
    }
    Sys.sleep(5) 
    for(i in 1:length(myinputs)){
      enable(myinputs[i])
    }
  })
}

shinyApp(ui, server)

enter image description here