I am looking for a way to implement disabling/abling functionality over my shinyUI elements. Here xiaodaigh gives a hint how to disable/able an actionButton
(see an image below) and this is my desired result, but the code does not do the trick with other gui elements I have tested (e.g. numericInput
).
(I know a conditionalPanel
feature, but this is not the effect I would like to have.)
I would strongly appreciate any suggestions, especially because I am not much familiar with JavaScript
.
The reason the code you link to in your question doesn't work for other input widgets is because different input widgets need different JavaScript calls to get disabled. Another problems is that when shiny creates an input element, sometimes the id that you provide is the ID of the actual HTML input tag while sometimes that ID gets given to a container of the input tag.
The shinyjs package has a disable
function that will work as-is with any shiny input. Disclaimer: I wrote that package.
Here is how you would achieve disabling a numericInput
like you asked
library(shiny) runApp(shinyApp( ui = fluidPage( shinyjs::useShinyjs(), numericInput("test", "Test", 5), actionButton("submit", "Choose") ), server = function(input, output, session) { observeEvent(input$submit, { shinyjs::disable("test") }) } ))
The code suggested by Dean Attali can actually be slightly modified to achieve enabling and disabling as requested. See example below, which activates or deactivates the slider dependent on the value given (if value exceeds max of range slider is deactivated).
library(shiny) runApp(shinyApp( ui = fluidPage( shinyjs::useShinyjs(), numericInput("val", "Choose value (max 10)", 5), sliderInput(inputId = "range", label = "Range", min = 0, max = 10, step = 0.1, value = c(0,2)) ), server = function(input, output, session) { observeEvent(input$val, { if(input$val <= 10){ shinyjs::enable("range") }else{ shinyjs::disable("range") } }) } ))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With