Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Shiny - disable / able shinyUI elements

Tags:

r

shiny

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.

enter image description here

like image 638
Marta Karas Avatar asked Oct 26 '13 20:10

Marta Karas


2 Answers

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")     })   } )) 
like image 66
DeanAttali Avatar answered Sep 24 '22 23:09

DeanAttali


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")             }         })     } ))  
like image 30
tokami Avatar answered Sep 22 '22 23:09

tokami